SpringBoot @WebMvcTest, autowiring RestTemplateBuilder

Alexandre picture Alexandre · Jan 16, 2017 · Viewed 7.7k times · Source

I've got a problem while testing a Spring Controller. I'm using the annotation @WebMvcTest in my test class. When I run the test, I get this error: No qualifying bean of type 'org.springframework.boot.web.client.RestTemplateBuilder' available

I'm using RestTemplate for other classes in my project so I've defined a bean in my main class:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.build();
}

To make it work, I have to define my restTemplate bean this way:

@Bean
public RestTemplate restTemplate() {
    return new RestTemplate();
}

Is it a problem with the annotation @WebMvcTest or did I miss something?

Thanks

Answer

Patrick Herrera picture Patrick Herrera · Mar 31, 2017

Yes, this does feel like a bug.
However you can solve it easily by adding @AutoConfigureWebClient to your test class along with the existing @WebMvcTest