f-lopes/spring-mvc-test-utils

[3.1.0] fails to find handler type and method

Closed this issue · 5 comments

I've tried using spring-mvc-test-utils v3.1.0 and found that both:

MockMvcRequestBuilderUtils.postForm("/myapp/path", formObject)

and

MockMvcRequestBuilders.post("/myapp/path").with(MockMvcRequestBuilderUtils.form(formObject))

fail to map to the handler that deals with "/myapp/path". Not sure if it's related but my controller has @RequestMapping("/myapp") on the class and @PostMapping({"/path"}) on the method. Also, the pojo in question uses lombok a bit like this:

@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class User {

    @NonNull
    private String emailAddress;

    @Nullable
    private String name;

    private Long level;
}

Thanks for your report.
Could you please elaborate on this error?

All this library does is map POJO fields to HTTP request parameters. This "handler" error seems to be related to a missing handler mapping. Is your handler (ie. PostMapping annotated method) discovered? -> Can you see the /myapp/path path in the startup logs? Spring logs the discovered mappings under the org.springframework.web.servlet.HandlerMapping.Mappings logger.

Also, did you register your controller with the MockMvc instance you use in your tests?

[...]
mockMvc = MockMvcBuilders.standaloneSetup(new UserController())
[...]

The test class is annotated with @WebMvcTest(MyController.class) and I autowire MockMvc. It's correctly picking up other methods which are being tested with standard MockMvcRequestBuilders.get but for some reason this method that has @PostMapping({"/path"}) just isn't playing ball.

I've been trying to recreate the problem in a test on spring-mvc-test-utils (hence PR #131) but it may just be a problem my end as you say. I'll try updating the logger and see what's happening there

Thanks for your reply 👍
Don't hesitate if you need more help 🙏

This has turned into a real pita. All tests for methods with @GetMapping work fine but if I test anything with @PostMapping then mockMvc fails to find a handler:

Handler:
             Type = null

this is the same whether I write the test using MockServerHttpRequest.post or MockMvcRequestBuilderUtils.postForm.

Thanks for your feedback. Feel free to share some code if you need help :)