小编Leo*_*oes的帖子

使用 Kotlin 在 Android 测试中声明 @BeforeClass 的正确方法是什么

我有一个像这样的 InstrumentedTest 类:

@RunWith(AndroidJUnit4::class)
class MyInstrumentedTest {
    @Inject
    private lateinit var myVar:MyType

    @BeforeClass
    fun method(){
        with(myVar){
        ...
Run Code Online (Sandbox Code Playgroud)

这会产生错误,因为method必须是静态的。如果我将 @BeforeClass 方法放入伴生对象中并使用 @JvmStatic 进行注释,则无法使用注入的 myVar。在一般情况下和在这种情况下,是否有更合适的方法使用 kotlin 使用 @BeforeClass ?

testing junit android kotlin

4
推荐指数
1
解决办法
2300
查看次数

标签 统计

android ×1

junit ×1

kotlin ×1

testing ×1