使用Kotlin在springboot测试中使用@Autowired构造函数与不使用@Autowired构造函数有什么区别

时间:2020-06-27 11:22:18

标签: spring-boot kotlin autowired junit5

我想在Spring Boot 2.3.0中使用JUnit5编写测试代码。

首先,我尝试使用构造函数进行依赖注入。

像下面一样

enter image description here

但是当我开始测试代码时,整个测试失败了。

错误日志为string

所以我以为即使我使用构造函数,Bean Injenction也失败了。

此外,即使我在Intellij IDE中使用了构造函数,也没有显示Bean图标。

但是当我在构造函数中添加capacity时,Bean Icon开始出现。

像下面一样

enter image description here

资源类是仅在测试代码中使用的bean。

我可以解决问题,但我真的不知道发生了什么。

我想知道No ParameterResolver registered for parameter blah blah blah...

请让我知道这个神奇的@Autowired constructor的工作原理

1 个答案:

答案 0 :(得分:3)

测试用例的构造函数上的

{index + 1}是一个信号,指示测试框架(在本例中为Jupiter SpringExtension)使用Spring上下文解析构造函数参数。没有注释,Jupiter会尝试通过其他机制找到参数解析器,但找不到任何解析器,因此会出错。

相关问题