【问题标题】:Kotlin Spring Boot Unit Test - adding in @TestExecutionListeners doesn't inject dependenciesKotlin Spring Boot 单元测试 - 添加 @TestExecutionListeners 不会注入依赖项
【发布时间】:2020-01-09 02:12:18
【问题描述】:

我正在尝试使用 Flyway 测试扩展库,它的说明之一是添加:

@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, 
                         FlywayTestExecutionListener.class })

所以在 Kotlin 中我有一些类似的东西:

 @RunWith(SpringRunner::class)
 @TestExecutionListeners(DependencyInjectionTestExecutionListener::class, 
                         FlywayTestExecutionListener::class )
 class MyControllerTest {
     @Autowired
     lateinit var dataSource : DataSource
 }

但由于某种原因,当我尝试在该类中运行测试时,我收到一条错误消息,指出 lateinit 属性尚未初始化。

我需要做什么/做一些特别的事情才能让它发挥作用吗?

【问题讨论】:

    标签: spring-boot kotlin dependency-injection junit4


    【解决方案1】:

    好吧,我终于找到了另外 1 个帮助我解决问题的帖子:How to persist enums as ordinals with Spring Boot and Cassandra?

    我需要的注释是:

    @TestExecutionListeners(
        listeners = [FlywayTestExecutionListener::class],
        mergeMode = TestExecutionListeners.MergeMode.MERGE_WITH_DEFAULTS
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 2021-06-19
      • 1970-01-01
      • 1970-01-01
      • 2018-01-11
      相关资源
      最近更新 更多