Room.inMemoryDatabaseBuilder() not found in instrumented test

北城余情 提交于 2019-12-11 00:19:07

问题


I'm trying to create an instrumented test for Room DB I have in my app, but I keep getting this error:

java.lang.NoSuchMethodError: No static method inMemoryDatabaseBuilder(Landroid/content/Context;Ljava/lang/Class;)Landroid/arch/persistence/room/RoomDatabase$Builder; in class Landroid/arch/persistence/room/Room; or its super classes (declaration of 'android.arch.persistence.room.Room' appears in /data/app/com.example.android-nhsbta60PW8T0UpasgtR0B==/base.apk)
at com.example.android.FooDaoTest.<init>(FooDaoTest.kt:40)
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
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:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2074)

This is my Gradle build configuration:

project:

buildscript {
    ext {
        kotlinVersion = '1.2.0'

        supportLibVersion = '26.1.0'
        archLibVersion = '1.0.0'

        ...

        // Test libs
        androidTestLibVersion = '1.0.1'
        espressoVersion = '3.0.1'
        powerMockVersion = '1.7.1'
    }

    repositories {
        google()
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

app:

android {
    compileSdkVersion 26
    buildToolsVersion '26.0.3'

    defaultConfig {
        applicationId "com.example.android"

        minSdkVersion 17
        targetSdkVersion 26

        versionCode 1
        versionName "0.1"

        ...

        multiDexEnabled true

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        debug {
            minifyEnabled true
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        release {
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:multidex:1.0.2'

    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlinVersion"

    ...

    implementation "android.arch.lifecycle:extensions:$archLibVersion"
    implementation "android.arch.persistence.room:runtime:$archLibVersion"
    kapt "android.arch.lifecycle:compiler:$archLibVersion"
    kapt "android.arch.persistence.room:compiler:$archLibVersion"

    ...

    // Test libs

    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:2.8.47'
    testImplementation "org.powermock:powermock-module-junit4:$powerMockVersion"
    testImplementation "org.powermock:powermock-api-mockito2:$powerMockVersion"
    testImplementation 'org.hamcrest:hamcrest-library:1.3'
    testImplementation "android.arch.core:core-testing:$archLibVersion"
    testImplementation "com.squareup.okhttp3:mockwebserver:3.9.1"

    androidTestImplementation "com.android.support.test:runner:$androidTestLibVersion"
    androidTestImplementation "com.android.support.test:rules:$androidTestLibVersion"
    androidTestImplementation("com.android.support.test.espresso:espresso-core:$espressoVersion", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestImplementation "com.android.support.test.espresso:espresso-contrib:$espressoVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-intents:$espressoVersion"
    androidTestImplementation "com.android.support.test.espresso:espresso-idling-resource:$espressoVersion"
    androidTestImplementation 'com.android.support.test.uiautomator:uiautomator-v18:2.1.3'

    androidTestUtil "com.android.support.test:orchestrator:$androidTestLibVersion"
}

And this is my instrumented test:

@RunWith(AndroidJUnit4::class)
class FooDaoTest {

    private lateinit var db: AppDatabase

    private lateinit var fooDao: FooDao


    @Before
    fun setUp() {
        db = Room.inMemoryDatabaseBuilder(InstrumentationRegistry.getContext(), AppDatabase::class.java).build()

        fooDao = db.fooDao()
    }

    @After
    fun closeDb() {
        // db.close()
    }

    @Test
    fun save() {
        ...
    }
}

I'm guessing the error has something to do with multidex, but I can't quite find why. Does anyone know why this is happening and how I can make this test work?


回答1:


Seems like it was some build issue I was having, I'm posting what fixed it for me in case it helps others:

I tried cleaning the project from Android Studio a lot of times and the issue persisted, however, when I cleaned the project from the command line using:

./gradlew clean

and then run the test again it worked.

UPDATE:

After some more testing, I found out that the real issue was with my Proguard configuration. I am using Proguard in the debug builds, which removes all the non-used classes/methods (Room.inMemoryDatabaseBuilder() being one of them). Once I disabled Proguard for the debug builds and cleaned up the project as specified above it started working. So to wrap up, the solution is:

  1. Disable Proguard for the debug build (or add a test Proguard configuration to the debug build):

Option 1:

buildTypes {
//      debug {
//          minifyEnabled true
//          useProguard false
//          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
//      }
        ...
}

Option 2:

buildTypes {
        debug {
            minifyEnabled true
            useProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            testProguardFiles 'proguard-rules-test.pro'
        }
        ...
}

The contents of proguard-rules-test.pro will depend on what you need for your app.

  1. Clean the project from the command line:

    ./gradlew clean



来源:https://stackoverflow.com/questions/47763453/room-inmemorydatabasebuilder-not-found-in-instrumented-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!