I want to test the response of my request.
I have a controller like this,
@RequestMapping("/user")
public class StandardController(@RequestBody Object body) {
@PostMapping(path=/info")
public ResponseEntity<String> getUserInfo(@Validated @RequestBody CustomDTO customDTO) throws customException {
try {
//process something with customDTO
} catch (Exception e) {
//throw exception
}
}
}
Now I have made one of the properties of CustomDTO
as @NotNull
. When I test the endpoint through Postman I will successfully get 400 as expected if I supply the required field as null value. But how do I test this scenario with Mockito?
CodePudding user response:
If you want to test Spring MVC controllers, don't use unit tests; you need to ensure that everything works correctly including mappings, validation, and serialization. Use MockMvc (in regular, not standalone mode), and if you like you can use Mockito to mock service dependencies.
CodePudding user response:
If you want to test your controller, you have two categories of test:
Unit test by using
spring
test slice.
Read this post also for more understanding.