Expect Bad Request with Spring 6.1

76 views Asked by At

I used to have this test that worked as expected:

mvc.perform(get(ENDPOINT)
   .queryParam("myparam", "100")
   andExpectAll(
      status().isBadRequest().
      content().contentType(APPLICATION_PROBLEM_JSON),
      //language=json
      content().json("""
                    {
                      "type": "/problems/validation-error",
                      "title": "The request parameters didn't validate",
                      "instance": "my-endpoint",
                      "status": 400,
                      "errors": [
                        {
                          "path":"myparam","message":"size must be between 0 and 10"
                        }
                      ]
                    }
                    """, true));

My API definition has the following parameter annotated: @RequestParam(required = false) @Size(max = 10)

However this does not work anymore after upgrading from Spring Boot 3.1.8 to Spring Boot 3.2.3 that comes with Spring 6.1.4. Now instead of being able to capture the request status, there's an exception thrown:

org.springframework.web.method.annotation.HandlerMethodValidationException: 400 BAD_REQUEST "Validation failure"
    at org.springframework.web.method.annotation.HandlerMethodValidator.applyArgumentValidation(HandlerMethodValidator.java:106)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:188)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:920)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:830)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1089)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:979)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:527)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
    at org.springframework.test.web.servlet.TestDispatcherServlet.service(TestDispatcherServlet.java:72)
    at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:614)
    at org.springframework.mock.web.MockFilterChain$ServletFilterProxy.doFilter(MockFilterChain.java:165)
    at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:132)  at org.springframework.web.method.annotation.HandlerMethodValidator.applyArgumentValidation(HandlerMethodValidator.java:106)

What's the quickest and nicest way to have this test working again? I could write an AdviceTrait for that exception but I doubt that's necessary any time one wants to check for a 400.

0

There are 0 answers