无法自动装配`WebTestClient` - 无需自动配置

时间:2018-01-12 12:50:30

标签: spring spring-boot spring-webflux

我们正在使用spring framework 5和spring boot 2.0.0.M6,我们也使用WebClient进行反应式编程。我们为我们的反应式休息端点创建了测试方法,因此我查找了一些关于如何操作的示例。我发现this一个或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.

所以似乎不存在自动配置。我使用的是错误版本还是这里有什么问题?

2 个答案:

答案 0 :(得分:24)

使用C:\>adb version Android Debug Bridge version 1.0.39 Revision 3db08f2c6889-android Installed as C:\Program Files (x86)\Android\android-sdk\platform-tools\adb.exe 注释为您的docsp = [['how', 'can', 'I', 'change', 'this', 'car'], ['I', 'can', 'delete', 'this', 'module']] ent = {'change' : 'action', 'car' : 'item', 'delete' : 'action'} result = [[(docsp[i][j], ent.get(docsp[i][j], 'O')) for j in range(len(docsp[i]))] \ for i in range(len(docsp))] # [[('how', 'O'), # ('can', 'O'), # ('I', 'O'), # ('change', 'action'), # ('this', 'O'), # ('car', 'item')], # [('I', 'O'), # ('can', 'O'), # ('delete', 'action'), # ('this', 'O'), # ('module', 'O')]] 测试类添加注释。这应该可以解决这个问题。

答案 1 :(得分:17)

可接受的答案将继续向我抛出该错误,相反,除了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>

然后使用标准的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();
    }

}