Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant autowire `WebTestClient` - no auto configuration

We are using spring framework 5 and spring boot 2.0.0.M6 and we are also using WebClient for reactive programming. We created test methods for our reactive rest endpoints and so I looked up for some example on how to do it. I found this one or this and many others which where all the same. They just autowire a WebTestClient. So I tried the same:

@Log @RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class MyControllerTest {      @Autowired     private WebTestClient webClient;      @Test     public void getItems() throws Exception {         log.info("Test: '/items/get'");          Parameters params = new Parameters("#s23lkjslökjh12", "2015-09-20/2015-09-27");          this.webClient.post().uri("/items/get")                 .accept(MediaType.APPLICATION_STREAM_JSON)                 .contentType(MediaType.APPLICATION_STREAM_JSON)                 .body(BodyInserters.fromPublisher(Mono.just(params), Parameters.class))                 .exchange()                 .expectStatus().isOk()                 .expectHeader().contentType(MediaType.APPLICATION_STREAM_JSON)                 .expectBody(Basket.class);     } } 

I cannot run this because I get the error:

Could not autowire. No beans of 'WebTestClient' type found. 

So it does not seem that there is a auto configuration existing. Do I use the wrong version or what is the matter here?

like image 732
Mulgard Avatar asked Jan 12 '18 12:01

Mulgard


People also ask

What is@ SpringBootTest annotation?

@SpringBootTest is a primary annotation to create unit and integration tests in Spring Boot applications. The annotation enables additional features such as custom environment properties, different web environment modes, random ports, TestRestTemplate and WebTestClient beans.

What is WebTestClient?

public interface WebTestClient. Client for testing web servers that uses WebClient internally to perform requests while also providing a fluent API to verify responses. This client can connect to any server over HTTP, or to a WebFlux application via mock request and response objects.

Which of the following annotations can be used for testing Spring Boot Application which creates the entire context without starting the server?

Spring Boot provides a @SpringBootTest annotation, which can be used as an alternative to the standard spring-test @ContextConfiguration annotation when you need Spring Boot features. The annotation works by creating the ApplicationContext used in your tests through SpringApplication .


2 Answers

Annotate your MyControllerTest test class with @AutoConfigureWebTestClient annotation. That should solve the issue.

like image 65
Lukáš Kořán Avatar answered Oct 02 '22 22:10

Lukáš Kořán


The accepted answer keeps throwing that error for me, instead I had to add the webflux starter in addition to the test starter in Spring Boot 2.0.3:

<dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-webflux</artifactId>     <scope>test</scope> </dependency>  <dependency>     <groupId>org.springframework.boot</groupId>     <artifactId>spring-boot-starter-test</artifactId>     <scope>test</scope> </dependency> 

Then use the standard web test annotations:

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) public class IntegrationTest {      @Autowired     private WebTestClient webClient;      @Test     public void test() {         this.webClient.get().uri("/ui/hello.xhtml")           .exchange().expectStatus().isOk();     }  } 
like image 22
Xtreme Biker Avatar answered Oct 02 '22 23:10

Xtreme Biker