I'm trying to test a Room database in Android, written in Kotlin, locally; that is not on an emulator and without instrumented testing. As I understand it this is possible using the Roboelectric framework but I'm having some trouble with this :
How does one select the appropriate class for the
RunWith(CLASS)decorator ?I understand
AndroidJUnit4is necessary for instrumented tests, given that this is a local test should I be usingJUnit4instead or some other derivative from RoboElectric.How should I correctly determine the
context?Trying
InstrumentationRegistry.getTargetContext()requires that I useInstrumentationRegistrywhich isn't available when testing locally or thattestcan not be imported when importingandroidx.test.platform.app.InstrumentationRegistryorandroid.support.test.InstrumentationRegistry. TryingApplicationProvider.getApplicationContext()claims the referencegetApplicationContextcan't be found. I'm also not sure where I should be importing eitherInstrumentationRegistryorApplicationProviderfrom.
In general I'm finding it tricky to determine the appropriate libraries to use in Java; all the documentation appears very version specific and assumes you magically know where to import a given class from where often such a classes appear in more then one library. Then package imported through gradle also seems to be well related but does not explicitly match the package being imported. If you have any general tips on this I would be quite keen to hear it.
My code so far is as follows:
package com.manaikan.airvendor.AirTime
import android.content.Context
import android.arch.persistence.room.Room
import androidx.test.platform.app.InstrumentationRegistry
import com.manaikan.airvendor.airtime.AirVendorDatabase
import com.manaikan.airvendor.airtime.BundleQuery
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import java.io.IOException
// Roboelectric
import androidx.test.core.app.ApplicationProvider
@RunWith(JUnit4::class)
class BundleTest {
private lateinit var queryset : ENTITYDao
private lateinit var database : APPLICATIONDatabase
@Before
fun setup()
{
val context = ApplicationProvider.getApplicationContext<Context>()
database = Room.inMemoryDatabaseBuilder(context, APPLICATIONDatabase::class.java)
.allowMainThreadQueries()
.build()
queryset = database.bundleQuery()
}
@After
@Throws(IOException::class)
fun tearDown()
{
database.close()
}
@Test
@Throws(Exception::class)
fun review()
{
}
}