Spring
WebTestClient
Autowiring
AutoConfiguration
Testing

Cant autowire WebTestClient - no auto configuration

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

WebTestClient is a Spring test client designed primarily for reactive applications and test slices that expose the right auto-configuration. If autowiring fails, the usual reason is that the test context was not created with the annotations or dependencies that actually register a WebTestClient bean. In other words, this is usually a test wiring problem, not a client API problem.

Know When WebTestClient Is Available

Spring does not inject WebTestClient into every test automatically. The client is commonly available in these situations:

  • A reactive application test using @SpringBootTest with @AutoConfigureWebTestClient.
  • A focused WebFlux test slice such as @WebFluxTest.
  • A manually constructed client bound to a server or router function.

A typical integration test looks like this:

java
1import org.junit.jupiter.api.Test;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
4import org.springframework.boot.test.context.SpringBootTest;
5import org.springframework.test.web.reactive.server.WebTestClient;
6
7@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
8@AutoConfigureWebTestClient
9class GreetingControllerTest {
10
11    @Autowired
12    private WebTestClient webTestClient;
13
14    @Test
15    void returnsGreeting() {
16        webTestClient.get()
17            .uri("/greeting")
18            .exchange()
19            .expectStatus().isOk();
20    }
21}

Without @AutoConfigureWebTestClient, the bean may not exist in the test context.

Use the Right Test Style for the Stack

WebTestClient is the natural fit for WebFlux. If your application is based on Spring MVC rather than WebFlux, MockMvc is often the more conventional tool.

That distinction matters because developers sometimes try to autowire WebTestClient in an MVC-only test setup and then assume auto-configuration is broken. In reality, the test slice may simply be the wrong one for that client.

For a WebFlux controller slice, use something like:

java
1import org.junit.jupiter.api.Test;
2import org.springframework.beans.factory.annotation.Autowired;
3import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
4import org.springframework.test.web.reactive.server.WebTestClient;
5
6@WebFluxTest
7class GreetingControllerTest {
8
9    @Autowired
10    private WebTestClient webTestClient;
11
12    @Test
13    void returnsGreeting() {
14        webTestClient.get()
15            .uri("/greeting")
16            .exchange()
17            .expectStatus().isOk();
18    }
19}

Build the Client Manually When Needed

If you do not want Spring to auto-configure the client, you can construct one yourself. This is useful for focused tests and for debugging whether the problem is really auto-configuration or simply bean availability.

java
1import org.junit.jupiter.api.Test;
2import org.springframework.test.web.reactive.server.WebTestClient;
3
4class ManualClientTest {
5
6    private final WebTestClient client = WebTestClient
7        .bindToServer()
8        .baseUrl("http://localhost:8080")
9        .build();
10
11    @Test
12    void callsServer() {
13        client.get().uri("/greeting").exchange().expectStatus().isOk();
14    }
15}

For application-context-driven tests, Spring also supports binding the client to the application context directly. That can be a good middle ground when you want framework wiring but not full auto-configuration magic.

Check the Dependencies

Make sure your test dependencies include the Spring Boot test starter, and for reactive testing ensure the WebFlux stack is actually present. A missing reactive dependency can prevent the relevant auto-configuration from loading.

In Gradle, the common baseline is:

groovy
1dependencies {
2    testImplementation 'org.springframework.boot:spring-boot-starter-test'
3    implementation 'org.springframework.boot:spring-boot-starter-webflux'
4}

If the application is not reactive, use the dependency set that matches your real runtime stack and choose the test client accordingly.

Common Pitfalls

The most common mistake is using @SpringBootTest without @AutoConfigureWebTestClient and expecting the client bean to appear automatically.

Another issue is mixing MVC and WebFlux expectations. If the app is MVC-based, MockMvc may be the correct test tool instead.

Developers also forget the reactive dependency itself. Without the right stack on the classpath, the auto-configuration conditions do not match.

Finally, do not assume a failed autowire means WebTestClient is broken. It usually means the test context was not shaped to provide that bean.

Summary

  • 'WebTestClient is not auto-wired into every Spring test by default.'
  • Use @AutoConfigureWebTestClient with @SpringBootTest when appropriate.
  • For controller slices, @WebFluxTest is a natural way to get WebTestClient.
  • Match the client to the application stack: WebFlux for WebTestClient, MVC often for MockMvc.
  • Build the client manually when you want to avoid context auto-configuration entirely.

Course illustration
Course illustration

All Rights Reserved.