10

I currently wrote a put request I wanted to test via WebTestClient. I followed some tutorials and adapted my case to it. Testing the request results in an error:

"NOSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.web.reactive.server.WebTestClient' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}"

I looked up some solutions on SO like this one: Cant autowire `WebTestClient` - no auto configuration but couldn't make it work, despite of the hints there.

Here is the test code:

@SpringBootTest
@AutoConfigureWebTestClient
public class DemonstratorApplicationTests {

private P4uServiceImpl p4uService = new P4uServiceImpl();

@Autowired
WebTestClient webTestClient;

@MockBean
ReqP4uAccount account;

@Test
void testPutAccount(){

    ReqP4uAccount request = p4uService.buildRequest("testAccount");

    this.webTestClient.put()
            .uri("/account")
            .contentType(MediaType.APPLICATION_JSON)
            .body(request, ReqP4uAccount.class)
            .exchange()
            .expectStatus().isOk()
            .expectBody(P4uAccount.class);
}
}

Has anyone an idea what's wrong with the test setup? Thx in advance

letsgetraw
  • 193
  • 1
  • 10
  • Did you validate that your pom.xml has the same dependencies (TEST) dependencies as the SO link you pasted? – JCompetence Jan 07 '22 at 14:25
  • Dependencies I included are: spring-boot-starter-web, spring-boot-starter-test, spring-boot-starter-webflux @SMA – letsgetraw Jan 07 '22 at 14:28

1 Answers1

20

The following works:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
    </dependency>

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class DemoApplicationTests {

    @Autowired
    WebTestClient webTestClient;

If you remove (webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT), it will fail.


If you look at the WebTestClientAutoConfiguration , you can see that it has @ConditionalOnClass({ WebClient.class, WebTestClient.class }) and that could be why it wont work, unless Springboot starts up the web application context during (webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

/**
 * Auto-configuration for {@link WebTestClient}.
 *
 * @author Stephane Nicoll
 * @author Andy Wilkinson
 * @since 2.0.0
 */
@Configuration(proxyBeanMethods = false)
@ConditionalOnClass({ WebClient.class, WebTestClient.class })
@AutoConfigureAfter({ CodecsAutoConfiguration.class, WebFluxAutoConfiguration.class })
@Import(WebTestClientSecurityConfiguration.class)
@EnableConfigurationProperties
public class WebTestClientAutoConfiguration {
JCompetence
  • 6,997
  • 3
  • 19
  • 26