Test for Android in the Kotlin kingdom

How can one do a simple test of a realm database in Android implementing a test in Kotlin?

I tried to adapt a snippet from java realm test on github in kotlin and got the following 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 run the test, I get:

org.junit.internal.runners.rules.ValidationError: The @Rule 'rule' must be public.

      

+1


source to share


2 answers


You can make the recipient of a rule publicly available like this:

@get: Rule
var rule = PowerMockRule()

      

Or you can mark it as a Java style field with annotation @JvmField

:



@JvmField @Rule
var rule = PowerMockRule()

      

You can find more details in this answer: fooobar.com/questions/60545 / ...

Ps. You should also consider doing it val

if you are not going to change its value anywhere.

+3


source


Realm java 4.1.0 has been released and Realm's main problem with Kotlin has been resolved !, You can test my sample project to see how you should configure the project or create classes. My sample is for testing a module in a Realm Server object with kotlin.



+1


source







All Articles