如何才能在Android中實現境界數據庫的簡單測試在Kotlin中實現測試?在kotlin境界的Android測試
我試圖適應從Java領域test on github一個片段科特林,得到了下面的代碼:
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))
}
}
但是,當我執行測試,我得到:
org.junit.internal.runners.rules.ValidationError: The @Rule 'rule' must be public.
謝謝,「@get規則」修復了這個問題。現在我得到java.lang.NoClassDefFoundError:org/mockito/cglib/proxy/Enhancer,但這是另一個問題。 – gilcu2