首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >无法自动连接`WebTestClient` -无自动配置

无法自动连接`WebTestClient` -无自动配置
EN

Stack Overflow用户
提问于 2018-01-12 20:50:30
回答 2查看 18K关注 0票数 36

我们使用spring Framework5和spring boot 2.0.0.M6,我们还使用WebClient进行反应式编程。我们为反应式rest端点创建了测试方法,因此我查找了一些如何做到这一点的示例。我找到了this one或this和许多其他的,它们都是一样的。他们只是自动创建了一个WebTestClient。所以我尝试了同样的方法:

代码语言:javascript
运行
复制
@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);
    }
}

我无法运行此命令,因为我收到以下错误:

代码语言:javascript
运行
复制
Could not autowire. No beans of 'WebTestClient' type found.

因此,似乎不存在自动配置。是我使用了错误的版本,还是这里出了什么问题?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-02-12 00:22:43

使用@AutoConfigureWebTestClient注释来注释您的MyControllerTest测试类。这应该可以解决问题。

票数 60
EN

Stack Overflow用户

发布于 2018-07-04 03:16:22

被接受的答案一直在为我抛出这个错误,而我不得不在Spring Boot 2.0.3中除了测试启动器之外,还添加了webflux starter:

代码语言:javascript
运行
复制
<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测试注释:

代码语言:javascript
运行
复制
@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();
    }

}
票数 40
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48226651

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档