Im getting this error when I try to test my Room DB. Tried with other solutions Ive found here (not many) and nothing seems to solve it.
If it may help, the default test automatically appears when you create your project (ExampleInstrumentedTest) does work.
This is the test file:
package com.example.weathered
import android.content.Context
import androidx.room.Room
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.example.weathered.database.CurrentWeatherDao
import com.example.weathered.database.ForecastWeatherDao
import com.example.weathered.database.WeatherDatabase
import com.example.weathered.entities.CurrentWeather
import org.hamcrest.Matchers.equalTo
import org.junit.After
import org.junit.Assert.*
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException
@RunWith(AndroidJUnit4::class)
class EntityReadWriteTest {
private lateinit var currentWeatherDao: CurrentWeatherDao
private lateinit var forecastWeatherDao: ForecastWeatherDao
private lateinit var database: WeatherDatabase
@Before
fun createDB() {
val context = ApplicationProvider.getApplicationContext<Context>()
database = Room.inMemoryDatabaseBuilder(context, WeatherDatabase::class.java).build()
currentWeatherDao = database.currentWeatherDao()
forecastWeatherDao = database.forecastWeatherDao()
}
@After
@Throws(IOException::class)
fun closeDB() {
database.close()
}
@Throws(Exception::class)
@Test
suspend fun writeCurrentWeatherAndReadUser() {
val id = 1
val currentWeather: CurrentWeather = TestUtil.createCurrentWeather(id).apply { }
currentWeatherDao.insert(currentWeather)
assertThat(
database.currentWeatherDao().get(id).value?.cityName,
equalTo(currentWeather.cityName)
)
}
}
And this is mi gradle file:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 28
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.weathered"
minSdkVersion 23
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
//testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation "androidx.room:room-testing:2.2.4"
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test:rules:1.2.0'
// Optional -- Hamcrest library
androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.survivingwithandroid:weatherlib:1.6.0'
implementation 'com.survivingwithandroid:weatherlib_volleyclient:1.6.0'
implementation 'com.mcxiaoke.volley:library:1.0.6@aar'
implementation 'androidx.room:room-runtime:2.2.4'
annotationProcessor 'androidx.room:room-compiler:2.2.4'
}
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire