不能自动装配 `WebTestClient` - 没有自动配置
Cant autowire `WebTestClient` - no auto configuration
我们正在使用 spring framework 5 和 spring boot 2.0.0.M6,我们还使用 WebClient
进行响应式编程。我们为我们的反应式休息端点创建了测试方法,所以我查找了一些关于如何做的例子。我发现 this one or 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.
因此似乎不存在自动配置。是我用错版本了还是怎么回事?
用 @AutoConfigureWebTestClient
注释注释您的 MyControllerTest
测试 class。这应该可以解决问题。
已接受的答案一直为我抛出该错误,相反,我不得不在 Spring Boot 2.0.3 中添加 webflux 启动器和测试启动器:
<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>
然后使用标准的网络测试注释:
@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();
}
}
我们正在使用 spring framework 5 和 spring boot 2.0.0.M6,我们还使用 WebClient
进行响应式编程。我们为我们的反应式休息端点创建了测试方法,所以我查找了一些关于如何做的例子。我发现 this one or 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.
因此似乎不存在自动配置。是我用错版本了还是怎么回事?
用 @AutoConfigureWebTestClient
注释注释您的 MyControllerTest
测试 class。这应该可以解决问题。
已接受的答案一直为我抛出该错误,相反,我不得不在 Spring Boot 2.0.3 中添加 webflux 启动器和测试启动器:
<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>
然后使用标准的网络测试注释:
@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();
}
}