Using @WebMvcTest
to test a controller that implements an interface and moreover has some annotations like @Secured
leads to a falsely used returnValueHandler, namely ModelAttributeMethodProcessor
instead of the correct RequestResponseBodyMethodProcessor
.
Debugging showed that when the handler is determined the RequestResponseBodyMethodProcessor
does not match because it tries to evaluate the @ResponseBody
annotation on a proxy instead of the actual Controller class.
If I remove either the interface definition or the @Secured
annotation it is working properly.
Moreover I've tested this with different Spring Boot versions including 2.4.2, 2.2.0.RELEASE, 2.0.0.RELEASE and 1.5.22.RELEASE
I've prepared and uploaded a small demo application that shows the issue: https://github.com/daviian/spring-webmvctest-interface-bug
EDIT: Issue also appears when using
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
instead of @WebMvcTest
Comment From: wilkinsona
Duplicates https://github.com/spring-projects/spring-framework/issues/23744. Setting proxyTargetClass = true
on @EnableGlobalMethodSecurity
fixes the problem.