【发布时间】: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。
【问题讨论】:
-
见github.com/spring-projects/spring-restdocs/issues/582 -> 文档确实是错误的。将在 Spring Restdocs 的 2.0.4 中修复。
标签: java spring spring-webflux junit5 spring-restdocs