我想在我的测试中使用WebTestClient。工作方式如下:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class ControllerTest {
@Autowired
private WebTestClient webTestClient;
@Test
public void webtestClient () {
assertNotNull(webTestClient);
}
}但是现在我想将WebTestClient注入到帮助类中:
@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?
发布于 2021-02-13 13:32:40
值得注意的是,我只需显式地指定AutoConfigureWebTestClient注释,就可以修复这个问题:
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class MyTestClass {
}发布于 2019-09-04 16:13:57
在测试中使用webTestClient之前,必须先构建它。
在下面使用,它会工作的
@Autowired
ApplicationContext context;
@Autowired
WebTestClient webTestClient;
@Before
public void setup() throws Exception {
this.webTestClient = WebTestClient.bindToApplicationContext(this.context).build();
}https://stackoverflow.com/questions/54289777
复制相似问题