[英]Spring Rest Docs with JUnit 5 and Webflux
I am unable to get a Spring Rest Docs test with JUnit 5 and Webflux working.我无法使用 JUnit 5 和 Webflux 进行 Spring Rest Docs 测试。
I have a working integration test with @WebFluxTest
like this:我有一个与@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 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.我还想知道是否需要@SpringBootTest
作为注释,或者@WebFluxTest(SomeController.class)
是否也应该工作。
I am using Spring Boot 2.1.3 with spring-restdocs-webtestclient
as dependency.我使用 Spring Boot 2.1.3 和spring-restdocs-webtestclient
作为依赖项。
Instead of injecting WebApplicationContext
use this:而不是注入WebApplicationContext
使用这个:
@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
.作为 Gilad Peleg 答案的替代方法,您只需将方法参数中的类型从WebApplicationContext
更改为ApplicationContext
。
Note also that instead of @SpringBootTest
, you can use @WebFluxTest(SomeController.class)
另请注意,您可以使用@WebFluxTest(SomeController.class)
而不是@SpringBootTest
@AutoConfigureRestDocs can also be used with WebTestClient. @AutoConfigureRestDocs 也可以与 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:您可以使用 @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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.