Документы Spring Rest с использованием JUnit 5 и Webflux - PullRequest
1 голос
/ 13 марта 2019

Я не могу получить тест Spring Rest Docs с работающим JUnit 5 и Webflux.

У меня есть рабочий интеграционный тест с @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);
    }
}

I сейчасхочу написать тест документации.Согласно документации, что-то вроде этого должно работать:

@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 Boot 2.1.3 с spring-restdocs-webtestclient в качестве зависимости.

Ответы [ 2 ]

1 голос
/ 14 марта 2019

В качестве альтернативы ответу Гилада Пелега вы можете просто изменить тип аргумента метода с WebApplicationContext на ApplicationContext.

Обратите внимание, что вместо @SpringBootTest вы можете использовать @WebFluxTest(SomeController.class)

1 голос
/ 13 марта 2019

Вместо введения WebApplicationContext используйте это:

    @Autowired
    private ApplicationContext context;

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