带有 SpringBootTest 的 MockMvc 在测试控制器时抛出异常 HttpMediaTypeNotSupportedException

问题描述 投票:0回答:3

我在集成测试和 MockMvc 的帮助下在 Spring RestController 中测试请求验证。

ControllerTest.java

@ExtendWith(MockitoExtension.class)
@SpringBootTest(classes = Controller.class)
@AutoConfigureMockMvc(addFilters = false)
class ControllerTest {

    private ObjectMapper objectMapper;

    @MockBean
    private Service service;

    @Autowired
    private MockMvc mockMvc;

    @BeforeEach
    void setUp() {
        objectMapper = new ObjectMapper();
    }

    @Test
    void createOrAdd_shouldReturnErrorResponseOnInvalidInput() throws Exception {
        Request request = Request.builder()
                .name("name<script>")
                .primaryEmail("[email protected]")
                .build();

        mockMvc.perform(MockMvcRequestBuilders.post("/api/create")
                        .accept(MediaType.APPLICATION_JSON)
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                        .content(objectMapper.writeValueAsString(request))
                .characterEncoding("utf-8"))
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }
}

Controller.java :

@Slf4j
@RestController
public class Controller {

    private final Service service;

    public Controller(Service service) {
        this.service = service;
    }

    @PostMapping(value = "/api/create", consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<GenericResponse<Response>> createOrAdd(@RequestBody @Valid Request request, Errors errors) {
        GenericResponse<Response> genericResponse = new GenericResponse<>();
        try {
            if (errors.hasErrors()) {
                throw new RequestParamsException(errors.getAllErrors());
            }
            Response response = service.createOrAdd(request);
            genericResponse.setData(response);
            return ResponseEntity.ok().body(genericResponse);
        } catch (RequestParamsException ex) {
            genericResponse.setErrors(ex.getErrors());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(genericResponse);
        }
    }

错误

WARN 17304 --- [           main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json;charset=utf-8' not supported]

MockHttpServletRequest:
      HTTP Method = POST
      Request URI = /api/create
       Parameters = {}
          Headers = [Content-Type:"application/json;charset=utf-8", Accept:"application/json", Content-Length:"162"]
             Body = {"name":"name<script>alert(1)</script>","primary_email_address":"[email protected]"}
    Session Attrs = {}

Handler:
             Type = com.org.controller.Controller
           Method = com.org.controller.Controller#createOrAdd(Request, Errors)

Async:
    Async started = false
     Async result = null

Resolved Exception:
             Type = org.springframework.web.HttpMediaTypeNotSupportedException

ModelAndView:
        View name = null
             View = null
            Model = null

FlashMap:
       Attributes = null

MockHttpServletResponse:
           Status = 415
    Error message = null
          Headers = [Accept:"application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, multipart/mixed, */*"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

java.lang.AssertionError: Status expected:<400> but was:<415>
Expected :400
Actual   :415

我在使用

Content-Type
拨打
Accept
时使用了正确的
Test.java
mockMvc
标头,但它仍然给出 HttpMediaTypeNotSupportedException。在
Accept
Content-Type
中尝试了很多组合,但仍然没有用。

我已经阅读了许多与此异常相关的 SO 问题,但在这里找不到问题所在。 仍然无法弄清楚为什么它说 HttpMediaTypeNotSupportedException。

更新:按照建议删除

addFilters = false
后,无法找到处理程序本身。

    MockHttpServletRequest:
          HTTP Method = POST
          Request URI = /api/create
           Parameters = {}
              Headers = [Content-Type:"application/json;charset=utf-8", Accept:"application/json", Content-Length:"162"]
                 Body = {"name":"name<script>alert(1)</script>","primary_email_address":"[email protected]"}
    Session Attrs = {org.springframework.security.web.csrf.HttpSessionCsrfTokenRepository.CSRF_TOKEN=org.springframework.security.web.csrf.DefaultCsrfToken@7a687d8d}   

     
Handler:
             Type = null

Async:
    Async started = false
     Async result = null
    
    Resolved Exception:
             Type = null
    
    ModelAndView:
            View name = null
                 View = null
                Model = null
    
    FlashMap:
           Attributes = null
    
MockHttpServletResponse:
           Status = 403
    Error message = Forbidden
          Headers = [X-Content-Type-Options:"nosniff", X-XSS-Protection:"1; mode=block", Cache-Control:"no-cache, no-store, max-age=0, must-revalidate", Pragma:"no-cache", Expires:"0", X-Frame-Options:"DENY"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

    
java.lang.AssertionError: Status expected:<400> but was:<403>
Expected :400
Actual   :403

请求

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
public class CreateAgencyRequest {

    @NotNull(message = "name can't be null")
    @JsonProperty(value = "name")
    @Pattern(regexp = REGEX_CONST, message = "name is not valid")
    private String name;

    @NotNull(message = "primary_email_address can't be null")
    @JsonProperty(value = "primary_email_address")
    private String primaryEmail;

}
spring spring-boot integration-testing spring-boot-test mockmvc
3个回答
1
投票

让我们看看你的测试。

@WebMvcTest(classes = Controller.class)
@AutoConfigureMockMvc(addFilters = false)
class ControllerTest {

    private ObjectMapper objectMapper;

    @MockBean
    private Service service;

    @Autowired
    private MockMvc mockMvc;

    @Test
    void createOrAdd_shouldReturnErrorResponseOnInvalidInput() throws Exception {
        Request request = Request.builder()
                .name("name<script>")
                .primaryEmail("[email protected]")
                .build();

        mockMvc.perform(MockMvcRequestBuilders.post("/api/create")
                        .accept(MediaType.APPLICATION_JSON)
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                        .content(objectMapper.writeValueAsString(request))
                .characterEncoding("utf-8"))
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }
}

首先,

@ExtendWith(MockitoExtension.class)
不会在您使用时添加任何内容,正确地,由 Spring 处理的
@MockBean
注释。所以你应该删除它。

接下来

@SpringBootTest
用于引导 完整的应用程序 以运行集成测试。你想要的是 web 的切片测试,所以而不是
@SpringBootTest
使用
@WebMvcTest
这将使你的测试更快。然后您也可以删除
@AutoConfigureMockMvc
,因为它是默认添加的。

你用

@AutoConfigureMockMvc(addFilters = false)
禁用了所有过滤器可能是因为你得到了403。您拥有的 403 是启用 CSRF(默认启用)而不是将其添加到请求中的结果。如果你不想要 CSRF(你可能想要它)要么在安全配置中禁用它,或者如果你想要它修改你的请求。

查看错误的罪魁祸首是

characterEncoding
被添加到内容类型中,因此您可能想要/应该删除它。

所有这些你的测试应该看起来像这样。


import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.*;

@WebMvcTest(Controller.class)
class ControllerTest {

    private ObjectMapper objectMapper = new ObjectMapper();

    @MockBean
    private Service service;

    @Autowired
    private MockMvc mockMvc;

    @Test
    void createOrAdd_shouldReturnErrorResponseOnInvalidInput() throws Exception {
        Request request = Request.builder()
                .name("name<script>")
                .primaryEmail("[email protected]")
                .build();

        mockMvc.perform(MockMvcRequestBuilders.post("/api/create")
                        .with(csrf()) // Add CSRF field for security
                        .accept(MediaType.APPLICATION_JSON)
                        .contentType(MediaType.APPLICATION_JSON_VALUE)
                        .content(objectMapper.writeValueAsString(request))
                .andExpect(MockMvcResultMatchers.status().isBadRequest());
    }
}

注意: 如果您还进行了身份验证,您可能还需要向请求中添加用户/密码,如此处解释


0
投票

如何在您的控制器端点添加

consumes
produces
?显然,端点不接受您在请求中发送的内容类型:

mockMvc.perform(MockMvcRequestBuilders.post("/api/create")
                        .accept(MediaType.APPLICATION_JSON)
                        .contentType(MediaType.APPLICATION_JSON_VALUE)

您告诉端点“嘿,我正在发送 JSON,我只接受返回的 JSON 响应”。但是端点说“看,在我接受的内容中,没有 JSON,所以请重新考虑!”

MockHttpServletResponse:
           Status = 415
    Error message = null
          Headers = [Accept:"application/octet-stream, text/plain, application/xml, text/xml, application/x-www-form-urlencoded, application/*+xml, multipart/form-data, multipart/mixed, */*"]
     Content type = null
             Body = 
    Forwarded URL = null
   Redirected URL = null
          Cookies = []

所以相应调整。

@RequestMapping(
    produces = {"application/json"}, 
    consumes = {"application/json"}, 
    method = RequestMethod.POST
)

所有其他配置、测试、注入都是无关紧要的。很好,但不能解决您的问题。


-2
投票

我认为你缺少@WebMvcTest(controllers = Controller.class)

© www.soinside.com 2019 - 2024. All rights reserved.