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

后端 未结 8 806
星月不相逢
星月不相逢 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: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.

提交回复
热议问题