【问题标题】:Spring Rest Docs with JUnit 5 and Webflux使用 JUnit 5 和 Webflux 的 Spring Rest Docs
【发布时间】:2019-03-13 09:10:09
【问题描述】:

我无法使用 JUnit 5 和 Webflux 进行 Spring Rest Docs 测试。

我有一个与@WebFluxTest 的有效集成测试,如下所示:

@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);
    }
}

我现在想编写一个文档测试。根据文档,这样的事情应该可以工作:

@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"));
    }
}

然而我得到:

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: {}

我还想知道是否需要 @SpringBootTest 作为注释,或者 @WebFluxTest(SomeController.class) 是否也应该工作。

我正在使用带有 spring-restdocs-webtestclient 作为依赖项的 Spring Boot 2.1.3。

【问题讨论】:

标签: java spring spring-webflux junit5 spring-restdocs


【解决方案1】:

不要注入WebApplicationContext,而是使用这个:

    @Autowired
    private ApplicationContext context;

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

【讨论】:

    【解决方案2】:

    作为 Gilad Peleg 答案的替代方案,您只需将方法参数中的类型从 WebApplicationContext 更改为 ApplicationContext

    另请注意,您可以使用@WebFluxTest(SomeController.class),而不是@SpringBootTest

    【讨论】:

    • 我觉得这个解决方案更优雅!
    【解决方案3】:

    根据docs

    @AutoConfigureRestDocs 也可以与 WebTestClient 一起使用。你可以 使用 @Autowired 注入它并像往常一样在测试中使用它 在使用 @WebFluxTest 和 Spring REST Docs 时会出现,如 下面的例子:

    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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-09
      • 2018-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-15
      • 2018-03-26
      • 1970-01-01
      相关资源
      最近更新 更多