Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"No converter found for return value" when running Spring Boot MockMvc test

I'm trying to create a MockMvc test of a Spring Boot controller. I specifically do not want the entire application context to be spun up, so I am restricting the context to the controller in question. However, the test fails with a 500 with the following log output:

2020-03-03 13:04:06.904  WARN 8207 --- [           main] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class main.endpoints.ResponseDto]

It appears that the Spring Boot context does not know how to find Jackson.

Here is the controller

@RestController
class MyController {
    @GetMapping("/endpoint")
    fun endpoint(): ResponseDto {
        return ResponseDto(data = "Some data")
    }
}

data class ResponseDto(val data: String)

The test is as follows:

@SpringBootTest(
    classes = [MyController::class],
    webEnvironment = SpringBootTest.WebEnvironment.MOCK
)
@AutoConfigureMockMvc
internal class MyControllerTest(@Autowired private val mockMvc: MockMvc) {
    @Test
    fun `should work`() {
        mockMvc.perform(MockMvcRequestBuilders.get("/endpoint").accept(MediaType.APPLICATION_JSON))
            .andExpect(
                content().json(
                    """
                        {
                            "data": "Some data"
                        }
                    """
                )
            )
    }
}

The build.gradle file includes the following dependencies:

    def jacksonVersion = "2.10.2"
    testImplementation("com.fasterxml.jackson.core:jackson-core:2.10.2")
    testImplementation("com.fasterxml.jackson.core:jackson-databind:2.10.2")
    testImplementation("com.fasterxml.jackson.core:jackson-annotations:2.10.2")

Any ideas on how to get this to work?

like image 825
user2615350 Avatar asked Nov 01 '25 07:11

user2615350


2 Answers

The solution is to annotate the class with @WebMvcTest rather than @SpringBootTest. This configures enough context that the test can interact via MockMvc with the controller.

Unfortunately, enabling @WebMvcTest has another side effect: all beans specified by @Bean-annotated methods in the configuration are also instantiated. This is a problem when those methods cannot be executed in a test environment (e.g. because they access certain environment variables).

To solve this, I added the annotation @ActiveProfiles("test") to the test and @Profile("!test") to each such annotated method. This suppresses the invocation of those methods and the test works.

like image 122
user2615350 Avatar answered Nov 04 '25 02:11

user2615350


@EnableWebMvc might solve your issue. According to Java Doc:

Adding this annotation to an @Configuration class imports the Spring MVC configuration from WebMvcConfigurationSupport,

e.g.:
 @Configuration
 @EnableWebMvc
 @ComponentScan(basePackageClasses = MyConfiguration.class)
 public class MyConfiguration {

 }

To customize the imported configuration, implement the interface WebMvcConfigurer and override individual methods, e.g.:

 @Configuration
 @EnableWebMvc
 @ComponentScan(basePackageClasses = MyConfiguration.class)
 public class MyConfiguration implements WebMvcConfigurer {

           @Override
           public void addFormatters(FormatterRegistry formatterRegistry) {
         formatterRegistry.addConverter(new MyConverter());
           }

           @Override
           public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
         converters.add(new MyHttpMessageConverter());
           }

 }
like image 36
Dega ASHOK KUMAR Avatar answered Nov 04 '25 02:11

Dega ASHOK KUMAR