Leo*_*oes 4 testing junit android kotlin
我有一个像这样的 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 ?
使用伴随对象是正确的方法:
companion object {
@BeforeClass
@JvmStatic
fun setupClass() {
// your class level setup logic here
}
}
Run Code Online (Sandbox Code Playgroud)
请记住,@BeforeClass 是静态的,并且在拥有测试对象实例之前执行。没有其他办法了。如果您想访问注入的变量,您应该在 @Before 方法中执行逻辑,该方法在每个 @Test 方法之前执行。
@Before
fun setup() {
// your setup logic here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2300 次 |
| 最近记录: |