43

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?

Mulgard
  • 9,877
  • 34
  • 129
  • 232

2 Answers2

71

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

Lukáš Kořán
  • 750
  • 7
  • 13
46

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();
    }

}
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • still does not work for me when run with junit 5... same exception. but the deps are like here. – ses Oct 01 '19 at 22:10
  • @ses, can you ask a question with your case? – Aritz Oct 02 '19 at 06:56
  • 5
    Dependency + `@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)` + `@AutoConfigureWebTestClient` worked. – Fırat Küçük Apr 03 '21 at 19:49
  • 1
    The documentation at https://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/autoconfigure/web/reactive/AutoConfigureWebTestClient.html suggests that only web flux applications are supported. So web flux dependency should be required. – thisisshantzz Nov 14 '22 at 16:55