When using an openApi generated interface, services don’t get injected in MockMvc tests. Let me show you.
Controller
@RestController
@Slf4j
@RequiredArgsConstructor
public class MyController implements MyApi {
private final CodeService codeService;
@Override
@PutMapping("/path")
public ResponseEntity<Void> putStuff(@RequestBody @Valid final Stuff stuff) {
// do stuff
return ResponseEntity.ok().build();
}
@ExceptionHandler(MethodArgumentNotValidException.class)
private ResponseEntity<ResponseWithError> handleException(final MethodArgumentNotValidException e, HttpServletRequest request) {
log.error(e.getMessage());
final ResponseWithError body = new ResponseWithError();
body.setTimestamp(TimeUtil.getCurrentTimestampString());
body.setId(UUID.randomUUID().toString());
body.setMessage(e.getMessage());
body.setPath(request.getRequestURI());
body.code(codeService.getErrors(e.getAllErrors()));
return ResponseEntity.badRequest().body(body);
}
}
MyApi
@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2023-11-15T16:01:27.640187200+01:00[Europe/Berlin]")
@Validated
@Tag(name = "stuff", description = "handles stuff")
public interface MyApi {
default Optional<NativeWebRequest> getRequest() {
return Optional.empty();
}
/**
* PUT /stuff : creates stuff
* creates stuff
*
* @param stuff (required)
* @return OK (status code 200)
* or CREATED (status code 201)
* or UNAUTHORIZED (status code 401)
* or BAD REQUEST (status code 400)
* or INTERNAL SERVER ERROR (status code 500)
*/
@Operation(
operationId = "putStuff",
summary = "creates stuff",
description = "creates stuff",
tags = {"stuff"},
responses = {
@ApiResponse(responseCode = "200", description = "OK"),
@ApiResponse(responseCode = "201", description = "CREATED"),
@ApiResponse(responseCode = "401", description = "UNAUTHORIZED", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ResponseWithError.class))
}),
@ApiResponse(responseCode = "400", description = "BAD REQUEST", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ResponseWithError.class))
}),
@ApiResponse(responseCode = "500", description = "INTERNAL SERVER ERROR", content = {
@Content(mediaType = "application/json", schema = @Schema(implementation = ResponseWithError.class))
})
},
security = {
@SecurityRequirement(name = "bearerAuth")
}
)
@RequestMapping(
method = RequestMethod.PUT,
value = "/path",
produces = {"application/json"},
consumes = {"application/json"}
)
default ResponseEntity<Void> putStuff(
@Parameter(name = "stuff", description = "", required = true) @Valid @RequestBody Stuff stuff
) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}
}
Test
@WebMvcTest(controllers = {MyController.class}, excludeAutoConfiguration = {SecurityAutoConfiguration.class})
class MyControllerTest {
@Autowired
private MockMvc mvc;
@MockBean
private CodeService codeService;
@Test
void invalidRequest() throws Exception {
// @GIVEN
final Stuff stuff = new Stuff();
stuff.setThings("");
final String body = new ObjectMapper().writeValueAsString(stuff);
given(codeService.getErrors(anyList())).willReturn(MY_VALIDATION_ERROR);
// @WHEN
mvc.perform(MockMvcRequestBuilders.put("/path")
.contentType("application/json")
.content(body));
// @THEN
// Assertions
}
}
- I’m using Spring-Boot 2.7.12
- CodeService is annotated with @Component
- MyApi is an OpenAPI-generated interface
When executing the test it triggers the exception and runs into the handler. All fine until it hits the line body.code(codeService.getErrors(e.getAllErrors()));
. Somehow codeService
does not get injected, but – and that’s the thing I don’t unterstand – when I don’t use the generated interface MyApi the codeService gets injected and the test runs as it should.
Can someone please explain, why the service does not get injected when I use the MyApi interface? And is there a way to get the test working with the generated api?
Edit
- It seems the
@Validated
annotation causes that behaviour. No matter if I annotate the controller directly or use the generated interface. Can someone explain why?
As it was commented by M. Deinum I changed the accessor to public. No it works!
Make your controller method
public
instead ofprivate
. You also don’t need the@Validated
on the interface so remove it. The@Validated
leads to a proxy being created, aprivate
method cannot be proxied and thus executes on the proxy which doesn’t have dependencies instead of being passed on to the actual wrapped object.