Delegate runner 'androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner' for AndroidJUnit4 could not be loaded

后端 未结 8 715
星月不相逢
星月不相逢 2021-01-17 11:30

I try to run Kotlin instrumentation tests for android.

In my app/build.gradle:

    android {
        dataBinding {
            enabled = true
               


        
相关标签:
8条回答
  • 2021-01-17 11:35

    In My case while using Java,I faced issue when I did below mistakes.

    1)I had made ActivityTestRule as private.

      All test cases should be public always.
    

    2)I didn't add static for BeforeClass and AfterClass methods

      Methods of @BeforeClass and @AfterClass should be static
    
      Ex)@BeforeClass
         public static void setUp(){
    
         }
    
    0 讨论(0)
  • 2021-01-17 11:38

    I just wasted all day on this issue

    0 讨论(0)
  • 2021-01-17 11:45

    For those who still have problems like this, This happens because Kotlin compiler wants to generate getter/setter properties. for example, I've got this error because I didn't mention @Rule annotation with @get:Rule.

    If we annotate properties like this with @get:... or @JvmField it could solve the problem.

    @get:Rule
    var mActivityRule = ActivityTestRule(MainActivity::class.java)
    

    Or

    @Rule
    @JvmField
    var mActivityRule = ActivityTestRule(MainActivity::class.java)
    

    @JvmField instructs the Kotlin compiler not to generate getters/setters for this property and expose it as a field.

    0 讨论(0)
  • 2021-01-17 11:45

    You don't have any functions annotated with @Test. This is likely the cause of the issue. I've run my own test using the AndroidJUnit4 runner on a class without @Test functions, and reconstructed this behavior.

    The AndroidJUnit4 JUnit4 runner is the cause of this issue, and despite its attempt to provide a detailed message (see the code where the exception is thrown), it gives you a wrong message.

    The following are more ways to achieve this error:

    @Test
    fun willThrowInitializationErrorBecauseReturnsNonUnitValue(): Int {
        return 0
    }
    
    @Test
    fun willThrowInitializationErrorBecauseTakesParameters(param: Int) {
    }
    

    The error is generated by the fact that you're writing JUnit4 style tests, and they're expected to conform to JUnit4 semantics, but it would be nice if the error message were more clear.

    To make matters worse, if one test function is messed up the runner won't run any tests because the InitializationError is thrown in the constructor of the AndroidJUnit4 class.

    0 讨论(0)
  • 2021-01-17 11:46

    The error message displayed when I delete the @Test method.

    You can try to put a @Test method and run

    @Test
    public void signInTest() {
    
    
    }
    
    0 讨论(0)
  • 2021-01-17 11:52

    just make sure that if your test is calling any suspend methods, you run your test with runBlocking{}. Solved the problem for me

    0 讨论(0)
提交回复
热议问题