【问题标题】:BeforeAll Method isn't being triggeredBeforeAll 方法没有被触发
【发布时间】:2020-08-02 19:25:31
【问题描述】:

我正在使用 spring boot、kotlin 和 junit5。我有一个测试课,当我使用@RunWith(SpringRunner::class) @BeforeAll 函数没有被触发,但是当我删除它时,它会为 lateinit 字段提供错误。

@RunWith(SpringRunner::class)
@SpringBootTest
class MagnificentClassTest {

  @MockBean
  lateinit var amazonSQS: AmazonSQSAsync

  @MockBean
  lateinit var simpleMessageListenerContainer: SimpleMessageListenerContainer

  @Autowired
  lateinit var magnificentClass: MagnificentClass


  @BeforeAll
  fun setUp() {
    insertApp()
    //insert some data to db
  }

 fun insertApp() {
      val entity = AppJdbc(
          APP_ID = 1
      )

      magnificentClass.insertApp(entity)
    }

【问题讨论】:

  • RunWith 是一种 JUnit 4 机制。 BeforeAll 是 JUnit5/Jupiter。他们不属于一起。此外,beforeAll 方法必须是静态的,除非您将实例生命周期更改为 PER_CLASS。

标签: spring-boot kotlin junit5


【解决方案1】:

BeforeAll 必须在静态方法上,因为每个测试类运行一次。

所以对于 Koltin,这看起来像:

@RunWith(SpringRunner::class)
@SpringBootTest
class MagnificentClassTest {

  @MockBean
  lateinit var amazonSQS: AmazonSQSAsync

  @MockBean
  lateinit var simpleMessageListenerContainer: SimpleMessageListenerContainer

  @Autowired
  lateinit var magnificentClass: MagnificentClass

  companion object {
            @BeforeAll
            @JvmStatic
            fun setUp() {

            }

        }
}

请查看https://dzone.com/articles/kotlin-and-junit-5-beforeall

【讨论】:

  • 已编辑问题,我还需要调用自动装配对象,我不能在伴随对象中调用它
  • 您不能在 BeforeAll 中执行此操作。在 BeforeEach 中执行并检查数据是否已插入
猜你喜欢
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
  • 2012-06-03
相关资源
最近更新 更多