小编Mhy*_*Mhy的帖子

Kotlin - 如何在 springBootTest 中管理 @BeforeClass 静态方法

我想在我的 springBootTest 中有一个 @BeforeClass 方法,它应该是静态的并在“伴随​​对象”中声明。

@RunWith(SpringRunner::class)
@SpringBootTest
@ActiveProfiles("test")
open class MyTest {
companion object {

    @Autowired
    lateinit var repo: MyRepository

    @BeforeClass
    @JvmStatic
    fun X() {
        user = User()
        repo.save(user)
    }

}
Run Code Online (Sandbox Code Playgroud)

另一方面,我应该在这个方法中使用一些 Autowired 组件,但正如这里提到的在静态上下文中是不可能的,我得到了这个错误:

lateinit property repo has not been initialized
Run Code Online (Sandbox Code Playgroud)

关于我应该如何处理这种情况的任何建议?

junit spring kotlin spring-boot spring-boot-test

6
推荐指数
1
解决办法
3880
查看次数

标签 统计

junit ×1

kotlin ×1

spring ×1

spring-boot ×1

spring-boot-test ×1