I have a problem with testing a Room database: when I run the test, I get this exception:
java.lang.RuntimeException: cannot find implementation for database.TicketDatabase. TicketDatabase_Impl does not exist at android.arch.persistence.room.Room.getGeneratedImplementation(Room.java:92) at android.arch.persistence.room.RoomDatabase$Builder.build(RoomDatabase.java:454) at com.sw.ing.gestionescontrini.DatabaseTest.setDatabase(DatabaseTest.java:36) at java.lang.reflect.Method.invoke(Native Method) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:27) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at org.junit.runner.JUnitCore.run(JUnitCore.java:115) at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1886)
Class DatabaseTest:
public class DatabaseTest { TicketDatabase database; DAO ticketDAO; @Before public void setDatabase() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { Context context = InstrumentationRegistry.getTargetContext(); database = Room.inMemoryDatabaseBuilder(context, TicketDatabase.class).build(); Method method = TicketDatabase.class.getDeclaredMethod("ticketDao()"); method.setAccessible(true); ticketDAO = (DAO) method.invoke(database, null); } }
gradle file:
apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion '26.0.2' defaultConfig { applicationId "com.sw.ing.gestionescontrini" minSdkVersion 15 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } defaultConfig { multiDexEnabled true } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.+' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' annotationProcessor 'android.arch.lifecycle:compiler:1.0.0' compile 'android.arch.persistence.room:rxjava2:1.0.0-rc1' testCompile'android.arch.persistence.room:testing:1.0.0-rc1' }
I don't really know what this implementation should be, I searched for a solution but all I found doesn't work for me. For instance, many found a solution adding kapt "android.arch.persistence.room..." but gradle doesn't recognize "kapt".
This method is deprecated. Deletes all rows from all the tables that are registered to this database as Database. entities .
Room is a Database Object Mapping library that makes it easy to access database on Android applications. Rather than hiding the details of SQLite, Room tries to embrace them by providing convenient APIs to query the database and also verify such queries at compile time.
The Room persistence library provides an abstraction layer over SQLite to allow for more robust database access while harnessing the full power of SQLite.
androidx.room.MultiInstanceInvalidationService. A Service for remote invalidation among multiple InvalidationTracker instances. This service runs in the main app process. All the instances of InvalidationTracker (potentially in other processes) has to connect to this service.
kapt
is for Kotlin.
First, add:
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
to your dependencies
closure.
Then, upgrade android.arch.persistence.room:rxjava2
and android.arch.persistence.room:testing
to 1.0.0
instead of 1.0.0-rc1
.
ref: https://developer.android.com/jetpack/androidx/releases/room
for Kotlin change the 'annotationProcessor' keyword to 'kapt' in gradle file.
kapt "android.arch.persistence.room:compiler:1.1.1"
and also add on top of the dependency file
apply plugin: 'kotlin-kapt'
ref: https://developer.android.com/jetpack/androidx/releases/room
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With