我们使用spring Framework5和spring boot 2.0.0.M6,我们还使用WebClient
进行反应式编程。我们为反应式rest端点创建了测试方法,因此我查找了一些如何做到这一点的示例。我找到了this one或this和许多其他的,它们都是一样的。他们只是自动创建了一个WebTestClient
。所以我尝试了同样的方法:
@Log
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class MyControllerTest {
@Autowired
private WebTestClient webClient;
@Test
public void getItems() throws Exception {
log.info("Test: '/items/get'");
Parameters params = new Parameters("#s23lkjslökjh12", "2015-09-20/2015-09-27");
this.webClient.post().uri("/items/get")
.accept(MediaType.APPLICATION_STREAM_JSON)
.contentType(MediaType.APPLICATION_STREAM_JSON)
.body(BodyInserters.fromPublisher(Mono.just(params), Parameters.class))
.exchange()
.expectStatus().isOk()
.expectHeader().contentType(MediaType.APPLICATION_STREAM_JSON)
.expectBody(Basket.class);
}
}
我无法运行此命令,因为我收到以下错误:
Could not autowire. No beans of 'WebTestClient' type found.
因此,似乎不存在自动配置。是我使用了错误的版本,还是这里出了什么问题?
发布于 2018-02-12 00:22:43
使用@AutoConfigureWebTestClient
注释来注释您的MyControllerTest
测试类。这应该可以解决问题。
发布于 2018-07-04 03:16:22
被接受的答案一直在为我抛出这个错误,而我不得不在Spring Boot 2.0.3中除了测试启动器之外,还添加了webflux starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
然后使用标准的web测试注释:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class IntegrationTest {
@Autowired
private WebTestClient webClient;
@Test
public void test() {
this.webClient.get().uri("/ui/hello.xhtml")
.exchange().expectStatus().isOk();
}
}
https://stackoverflow.com/questions/48226651
复制相似问题