Spring 使用 JUnit 5 和 Webflux 的 Rest 文档
Spring Rest Docs with JUnit 5 and Webflux
我无法使用 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 Boot 2.1.3,spring-restdocs-webtestclient
作为依赖项。
而不是注入 WebApplicationContext
使用这个:
@Autowired
private ApplicationContext context;
@BeforeEach
void setUp(RestDocumentationContextProvider restDocumentation) {
client = WebTestClient.bindToApplicationContext(context)
.configureClient()
.filter(documentationConfiguration(restDocumentation))
.build();
}
作为 Gilad Peleg 答案的替代方法,您只需将方法参数中的类型从 WebApplicationContext
更改为 ApplicationContext
。
另请注意,您可以使用 @WebFluxTest(SomeController.class)
而不是 @SpringBootTest
根据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;
}
我无法使用 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 Boot 2.1.3,spring-restdocs-webtestclient
作为依赖项。
而不是注入 WebApplicationContext
使用这个:
@Autowired
private ApplicationContext context;
@BeforeEach
void setUp(RestDocumentationContextProvider restDocumentation) {
client = WebTestClient.bindToApplicationContext(context)
.configureClient()
.filter(documentationConfiguration(restDocumentation))
.build();
}
作为 Gilad Peleg 答案的替代方法,您只需将方法参数中的类型从 WebApplicationContext
更改为 ApplicationContext
。
另请注意,您可以使用 @WebFluxTest(SomeController.class)
@SpringBootTest
根据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;
}