春季测试-WebTestClient无法自动装配。找不到“ WebTestClient”类型的bean

时间:2019-01-21 12:15:01

标签: java spring spring-boot testing intellij-idea

我想在测试中使用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”类型的bean。更多...   (Strg + F1)

新信息:该测试在Intellij中运行良好,但在使用maven运行时却没有。

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

如何在Helper类上使用WebTestClient?

2 个答案:

答案 0 :(得分:3)

对于它的价值 - 我能够通过简单地明确指定 AutoConfigureWebTestClient 注释来解决这个问题:

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureWebTestClient
public class MyTestClass {

}

答案 1 :(得分:0)

您必须先构建webTestClient才能在测试中使用它
在下面使用,它将起作用

UPDATE yourtable SET somecol = somecol || 'text' WHERE somecondition;