在领域的Kotlin中进行Android测试 [英] Android test in kotlin of realm
问题描述
如何通过在Kotlin中实施测试来对Android中的领域数据库进行简单测试?
How can be done a simple test of a realm database in Android implementing the test in Kotlin?
I attempted to adapt a fragment from java realm test on github to kotlin and got the next code:
import io.realm.Realm
import io.realm.log.RealmLog
import org.hamcrest.CoreMatchers
import org.junit.Assert
import org.junit.Test import org.junit.Before import org.junit.Rule
import org.mockito.Mockito.`when`
import org.powermock.api.mockito.PowerMockito
import org.powermock.modules.junit4.rule.PowerMockRule
class DBTest {
@Rule
var rule = PowerMockRule()
lateinit internal var mockRealm: Realm
@Before
fun setup() {
PowerMockito.mockStatic(RealmLog::class.java)
PowerMockito.mockStatic(Realm::class.java)
val mockRealm = PowerMockito.mock(Realm::class.java)
`when`(Realm.getDefaultInstance()).thenReturn(mockRealm)
this.mockRealm = mockRealm
}
@Test
fun shouldBeAbleToGetDefaultInstance() {
Assert.assertThat(Realm.getDefaultInstance(), CoreMatchers.`is`(mockRealm))
}
}
但是当我执行测试时,我得到了:
But when I execute the test I get:
org.junit.internal.runners.rules.ValidationError: The @Rule 'rule' must be public.
推荐答案
您可以像这样将规则的获取者公开:
You can make the getter of the rule public like so:
@get: Rule
var rule = PowerMockRule()
或者您可以使用@JvmField
批注将其标记为Java样式字段:
Or you can mark it as a Java style field with the @JvmField
annotation:
@JvmField @Rule
var rule = PowerMockRule()
您可以在此答案中找到更多详细信息: https://stackoverflow.com/a/32827600/4465208
You can find more details in this answer: https://stackoverflow.com/a/32827600/4465208
Ps.如果您不打算在任何地方更改其值,也应该考虑将其设为val
.
Ps. You should also consider making it a val
if you don't intend on changing its value anywhere.
这篇关于在领域的Kotlin中进行Android测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!