【问题标题】:Kotlin Autowired problems - lateinit (obv)Kotlin Autowired 问题 - lateinit (obv)
【发布时间】:2020-08-14 16:53:52
【问题描述】:

我对 kotlin 比较陌生,我无法理解 spring 自动装配

kotlin.UninitializedPropertyAccessException: lateinit property applicationContext has not been initialized

运行时出现上述错误:

@SpringBootTest
internal class MeterResourceAdapterTest () {

    @Autowired
    private lateinit var applicationContext: ApplicationContext

@Test
    fun toMeterReadingResourceList() {
        print(applicationContext.applicationName)
.....//I hope my implementation can be ignored
}

这里发生了什么?顺便说一句,为什么需要 lateinit,以及“它”何时被初始化?

【问题讨论】:

  • 我只是尝试运行一个最小的示例来重现您的问题,但它运行良好。如果我不得不猜测,您的依赖项存在一些问题,导致无法正确创建 applicationContext。您应该提供更多信息 - 您的应用程序运行正常吗?你有测试属性吗?

标签: spring spring-boot unit-testing kotlin lateinit


【解决方案1】:

@Stav Shamir 的评论让我得出了这个决议。

我不能确定是什么原因造成的,但几乎可以肯定这与我的 pom.xml 中的 junit 的旧版本有关。当我通过初始化程序创建一个新项目时,然后将该 pom 的内容( )复制到我的 pom 中;一切顺利。

【讨论】:

    猜你喜欢
    • 2020-07-24
    • 2023-03-13
    • 2017-12-25
    • 2016-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    相关资源
    最近更新 更多