Migrating Junit4 tests to androidx: What causes 'delegate runner could not be loaded'?

后端 未结 19 1803
长情又很酷
长情又很酷 2020-12-05 06:35

I am migrating my app to androidx, I can\'t seem to get my unit tests working. I took example from Google\'s AndroidJunitRunnerSample, which has been updated to use the new

相关标签:
19条回答
  • 2020-12-05 06:47

    Changing

    @Test
    void someTest() {
        // Testing here
    }
    

    to

    @Test
    public void someTest() {
        // Testing here
    }
    

    works for me.

    0 讨论(0)
  • 2020-12-05 06:48

    I gut this error for simply set fun as private, removing this solved this for me.

    0 讨论(0)
  • 2020-12-05 06:48

    AndroidJUnit4 DOESN'T SUPPORT in the functions with @Test annotations neither in the class nor in the superclass:

    • the parameters: @Test fun dontWork(param: String) {}
    • private access modifiers: @Test private fun dontWork2() {}

    Note: Without @Test annotations the above is allowed


    An expected way could be:

    ClassTest.kt

    import androidx.test.ext.junit.runners.AndroidJUnit4
    import org.junit.Test
    import org.junit.runner.RunWith
    
    @RunWith(AndroidJUnit4::class)
    class ClassTest : SuperTest() {
    
        @Test
        fun test() {}
    
        private fun allowedWithoutAnnotation(paramAllowed: String) {}
    
    }
    

    build.gradle (:mobile)

    defaultConfig {
        ...
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    
    
    dependencies {
        ...
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test.ext:junit:1.1.1'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
        androidTestImplementation 'androidx.arch.core:core-testing:2.1.0'
    } 
    

    GL

    0 讨论(0)
  • 2020-12-05 06:50

    You can use @JvmField. From documentation

    Instructs the Kotlin compiler not to generate getters/setters for this property and expose it as a field

    @Rule
    @JvmField
    val activityActivityTestRule = ActivityScenarioRule<MainActivity>(MainActivity::class.java)
    
    0 讨论(0)
  • 2020-12-05 06:50

    Maybe you haven't updated the runner on the gradle config file?

    defaultConfig {
        ...
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    

    Also, AndroidStudio 3.2 has an option to automate the migration of your dependencies to AndroidX (Refactor -> Migrate to AndroidX...) that did that for me.

    0 讨论(0)
  • 2020-12-05 06:53

    You need to make sure that any class marked with the @BeforeClass annotation is public static . For example:

    import org.junit.BeforeClass;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    
    import androidx.test.ext.junit.runners.AndroidJUnit4;
    
    @RunWith(AndroidJUnit4.class)
    public class EntityParcelTest {
    
        @BeforeClass
        public static void createEntities() {
            // Setup...
        }
    
        @Test
        public void someTest() {
            // Testing here
        }
    
    0 讨论(0)
提交回复
热议问题