Cant autowire WebTestClient - no auto configuration
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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
@SpringBootTestwith@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:
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:
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.
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:
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
- '
WebTestClientis not auto-wired into every Spring test by default.' - Use
@AutoConfigureWebTestClientwith@SpringBootTestwhen appropriate. - For controller slices,
@WebFluxTestis a natural way to getWebTestClient. - Match the client to the application stack: WebFlux for
WebTestClient, MVC often forMockMvc. - Build the client manually when you want to avoid context auto-configuration entirely.
Related reading
- Can't avoid hibernate logging SQL to console with Spring Boot and Logback
- Can't compile project when I'm using Lombok under IntelliJ IDEA
- Can't create handler inside thread that has not called Looper.prepare()
- Can't execute jar- file 'no main manifest attribute
- Changing names of parameterized tests
- Checking that a List is not empty in Hamcrest
- Can't find Nullable inside javax.annotation.
- Can't set JPA naming strategy after configuring multiple data sources Spring 1.4.1 / Hibernate 5.x

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.