测试框架意外退出 - ClassNotFound 异常

Test framework quit unexpectedly - ClassNotFound Exception

经过长时间调试和重构我的代码后,我想重新运行一些测试。我遇到的问题是我总是得到以下错误:

"C:\Program Files\Android\Android Studio\jre\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\Android\Android Studio\lib\idea_rt.jar=50253:C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.CommandLineWrapper C:\Users\NAME\AppData\Local\Temp\idea_classpath1240585070 com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.example.app.validator.BTValidatorTest
`CommandLineWrapper` is ill-suited for launching apps on Java 9+.
If the run configuration uses "classpath file", please change it to "@argfile".
Otherwise, please contact support.

Process finished with exit code 1

这是我的一个示例测试:

@RunWith(RobolectricTestRunner::class)
@Config(maxSdk = Build.VERSION_CODES.P, minSdk = Build.VERSION_CODES.P)
class BTValidatorTest {

    private val context = InstrumentationRegistry.getInstrumentation().targetContext
    private val formValidator = FormValidator(context)

    private lateinit var btValidator: BtValidator

    @Before
    fun setUp() {
        btValidator = BtValidator(
            formValidator
        )
    }

    @Test
    fun `bt number should be set correctly`() {
        btValidator.btNumber.value = "1234567"
        assertEquals("BT-1234567", btValidator.combinedBtNumber())
    }

    @Test
    fun `bt number error should be shown`() {
        btValidator.btNumber.value = ""
        assertEquals(false, btValidator.isBtNumberValid())
    }

    @Test
    fun `created device info should be set correctly`() {
        val product = "Vor 2017"
        val btNumber = 1234567

        val deviceInfo = DeviceInfo(
            product,
            btNumber
        )

        btValidator.product = product
        btValidator.btNumber.value = btNumber.toString()

        assertEquals(btValidator.createDeviceInfo(), deviceInfo)
    }

}

依赖关系

defaultConfig {
    applicationId "com.example.app"
    minSdkVersion 21
    targetSdkVersion 30
    versionCode 1
    versionName "0.1"

    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

    androidTestImplementation "androidx.test.ext:junit:1.1.2"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"

    testImplementation "junit:junit:4.13.2"
    testImplementation "androidx.test.ext:junit:1.1.2"
    testImplementation "androidx.test:core:1.3.0"
    testImplementation "org.robolectric:robolectric:4.5.1"

    testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.0-native-mt"
    testImplementation "androidx.arch.core:core-testing:2.1.0"

idea.log 文件

2021-06-15 11:12:20,886 [  60961]   INFO - ild.invoker.GradleBuildInvoker - About to execute Gradle tasks: [:app:generateDebugSources, :app:compileDebugSources, :app:createMockableJar, :app:compileDebugUnitTestSources] 
2021-06-15 11:12:20,901 [  60976]   INFO - s.plugins.gradle.GradleManager - Instructing gradle to use java from C:/Program Files/Android/Android Studio/jre 
2021-06-15 11:12:21,529 [  61604]   INFO - ild.invoker.GradleBuildInvoker - Build command line options: [-Pandroid.injected.invoked.from.ide=true, -Pandroid.injected.studio.version=202.7660.26.42.7351085, -Pandroid.injected.attribution.file.location=C:\Users\NAME\AndroidStudioProjects\RSB3000\.gradle, -Pandroid.injected.enableStableIds=true] 
2021-06-15 11:12:21,537 [  61612]   INFO - xecution.GradleExecutionHelper - Passing command-line args to Gradle Tooling API: -Pandroid.injected.invoked.from.ide=true -Pandroid.injected.studio.version=202.7660.26.42.7351085 -Pandroid.injected.attribution.file.location=C:\Users\NAME\AndroidStudioProjects\example\.gradle -Pandroid.injected.enableStableIds=true 
2021-06-15 11:12:29,306 [  69381]   WARN -       GradleBuildOutputUtil.kt - Failed to read Json output file from C:\Users\NAME\AndroidStudioProjects\RSB3000\app\build\outputs\apk\debug\output-metadata.json. Build may have failed. 
2021-06-15 11:12:29,460 [  69535]   INFO - ild.invoker.GradleBuildInvoker - Gradle build finished in 7 s 937 ms

我进行了更多研究,发现当 class Class.forName("java.lang.Module"); 不是 found:

时会发生此错误
try {
      Class.forName("java.lang.Module");
      System.err.println(
        "`CommandLineWrapper` is ill-suited for launching apps on Java 9+.\n" +
        "If the run configuration uses \"classpath file\", please change it to \"@argfile\".\n" +
        "Otherwise, please contact support.");
      System.exit(1);
    }
    catch (ClassNotFoundException ignored) { }

好的,我已经通过更改默认的 junit 配置解决了我的问题:

之前

之后

第一次改这个然后运行测试,我花了4分钟才完成测试