Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test framework quit unexpectedly - ClassNotFound Exception

After a long time and some debugging and refactoring of my code, I wanted to rerun some of my tests. The problem I encounter is that I always get the following error:

"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

Here is an example test of mine:

@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)
    }

}

Dependencies

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 file

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

I researched a bit more and found out that this error occurs, when a class Class.forName("java.lang.Module"); is not 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) { }
like image 940
Andrew Avatar asked Jan 24 '23 08:01

Andrew


1 Answers

Okay, I've managed to solve my problem by changing the default junit configuration:

Before

enter image description here

After

enter image description here

Changing this the first time and then running test, it took me a wop pin 4 minutes to finish the tests

like image 62
Andrew Avatar answered Feb 04 '23 04:02

Andrew