简体   繁体   中英

Spring Rest Docs with JUnit 5 and Webflux

I am unable to get a Spring Rest Docs test with JUnit 5 and Webflux working.

I have a working integration test with @WebFluxTest like this:

@WebFluxTest(SomeController.class)
class SomeControllerTest {

    @Autowired
    private WebTestClient testClient;
    @MockBean
    private SomeService service;

    @Test
    void testGetAllEndpoint() {

        when(service.getAll())
                .thenReturn(List.of(new Machine(1,"Machine 1", "192.168.1.5", 9060)));

        testClient.get().uri("/api/machines")
                  .exchange()
                  .expectStatus().isOk()
                  .expectBodyList(Machine.class)
                  .hasSize(1);
    }
}

I now want to write a documentation test. According to the docs, something like this should work:

@SpringBootTest
@ExtendWith({RestDocumentationExtension.class, SpringExtension.class})
class SomeControllerDocumentation {

    private WebTestClient testClient;
    @MockBean
    private SomeService service;

    @BeforeEach
    public void setUp(WebApplicationContext webApplicationContext,
                      RestDocumentationContextProvider restDocumentation) {
        this.testClient = WebTestClient.bindToApplicationContext(webApplicationContext)
                                       .configureClient()
                                       .filter(documentationConfiguration(restDocumentation))
                                       .build();
    }

    @Test
    void testGetAllEndpoint() {

        when(service.getMachines())
                .thenReturn(List.of(new Machine(1, "Machine 1", "192.168.1.5", 9060)));

        testClient.get().uri("/api/machines")
                  .accept(MediaType.APPLICATION_JSON)
                          .exchange().expectStatus().isOk()
                          .expectBody().consumeWith(document("machines-list"));
    }
}

I however get:

org.junit.jupiter.api.extension.ParameterResolutionException: 
Failed to resolve parameter [org.springframework.web.context.WebApplicationContext webApplicationContext] 

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'org.springframework.web.context.WebApplicationContext' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

I am also wondering if @SpringBootTest is needed as annotation or if @WebFluxTest(SomeController.class) is also supposed to work.

I am using Spring Boot 2.1.3 with spring-restdocs-webtestclient as dependency.

Instead of injecting WebApplicationContext use this:

    @Autowired
    private ApplicationContext context;

    @BeforeEach
    void setUp(RestDocumentationContextProvider restDocumentation) {
        client = WebTestClient.bindToApplicationContext(context)
            .configureClient()
            .filter(documentationConfiguration(restDocumentation))
            .build();
    }

As an alternative to the answer of Gilad Peleg, you can just change the type in the method argument from WebApplicationContext to ApplicationContext .

Note also that instead of @SpringBootTest , you can use @WebFluxTest(SomeController.class)

According to docs :

@AutoConfigureRestDocs can also be used with WebTestClient. You can inject it by using @Autowired and use it in your tests as you normally would when using @WebFluxTest and Spring REST Docs, as shown in the following example:

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.restdocs.AutoConfigureRestDocs;
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.reactive.server.WebTestClient;

import static org.springframework.restdocs.webtestclient.WebTestClientRestDocumentation.document;

@RunWith(SpringRunner.class)
@WebFluxTest
@AutoConfigureRestDocs
public class UsersDocumentationTests {

    @Autowired
    private WebTestClient webTestClient;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM