首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >弹簧测试- WebTestClient不能自动测试。没有找到'WebTestClient‘类型的bean

弹簧测试- WebTestClient不能自动测试。没有找到'WebTestClient‘类型的bean
EN

Stack Overflow用户
提问于 2019-01-21 12:15:01
回答 2查看 1.7K关注 0票数 2

我想在我的测试中使用WebTestClient。工作方式如下:

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class ControllerTest {

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void webtestClient () {
        assertNotNull(webTestClient);
    }
}

但是现在我想将WebTestClient注入到帮助类中:

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class ControllerTest {

    @Autowired
    private Helper helper;

    @Test
    public void webtestClient () {
        helper.check();
    }
}

@Component
public class Helper {
    @Autowired
    private WebTestClient webTestClient;

    public void check() {
        assertNotNull(webTestClient);
    }
}

也很管用。但是Intellij显示了一个错误:

无法自动布线。没有找到'WebTestClient‘类型的豆子。更多...(Strg+F1)

新信息:测试在Intellij中运行良好,但在使用maven运行时却不是这样。

下面是一个带有问题的测试项目:https://github.com/kicktipp/demo

如何在我的助教课上使用WebTestClient?

EN

回答 2

Stack Overflow用户

发布于 2021-02-13 13:32:40

值得注意的是,我只需显式地指定AutoConfigureWebTestClient注释,就可以修复这个问题:

代码语言:javascript
复制
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class MyTestClass {

}
票数 4
EN

Stack Overflow用户

发布于 2019-09-04 16:13:57

在测试中使用webTestClient之前,必须先构建它。

在下面使用,它会工作的

代码语言:javascript
复制
    @Autowired
    ApplicationContext context;

    @Autowired
    WebTestClient webTestClient;

    @Before
    public void setup() throws Exception {

        this.webTestClient = WebTestClient.bindToApplicationContext(this.context).build();
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54289777

复制
相关文章

相似问题

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