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?
@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.
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.
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 .
Annotate your MyControllerTest
test class with @AutoConfigureWebTestClient
annotation. That should solve the issue.
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(); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With