Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I try to run Kotlin instrumentation tests for android.

In my app/build.gradle:

    android {
        dataBinding {
            enabled = true
        }
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.myproject"
            minSdkVersion 18
            targetSdkVersion 28
            versionCode 6
            versionName "0.0.7"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }

        sourceSets {
            main.java.srcDirs += 'src/main/kotlin'
            androidTest.java.srcDirs += 'src/androidTest/kotlin'
        }

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0-alpha02'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.2-alpha02'
    androidTestImplementation 'androidx.test:runner:1.1.2-alpha02'

In folder /app/src/androidTest/kotlin/com/myproject/ I has Kotlin test:

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import org.junit.Rule
import org.junit.runner.RunWith

import androidx.test.rule.ActivityTestRule
import com.myproject.ui.activity.TradersActivity
import org.junit.Before


@RunWith(AndroidJUnit4::class)
@SmallTest
class TradersActivityTest {

    private lateinit var stringToBetyped: String

    @get:Rule
    var activityRule: ActivityTestRule<TradersActivity> = ActivityTestRule(TradersActivity::class.java)

    @Before
    fun initValidString() {
        // Specify a valid string.
        stringToBetyped = "Espresso"
    }

}

but when I run test I get error:

$ adb shell am instrument -w -r   -e debug false -e class 'com.myproject.TradersActivityTest' com.myproject.debug.test/androidx.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests

java.lang.RuntimeException: Delegate runner 'androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner' for AndroidJUnit4 could not be loaded.
at androidx.test.ext.junit.runners.AndroidJUnit4.throwInitializationError(AndroidJUnit4.java:92)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:82)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:51)
at androidx.test.ext.junit.runners.AndroidJUnit4.<init>(AndroidJUnit4.java:46)
at java.lang.reflect.Constructor.newInstance(Native Method)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at androidx.test.internal.runner.junit4.AndroidAnnotatedBuilder.runnerForClass(AndroidAnnotatedBuilder.java:63)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at androidx.test.internal.runner.AndroidRunnerBuilder.runnerForClass(AndroidRunnerBuilder.java:153)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at androidx.test.internal.runner.TestLoader.doCreateRunner(TestLoader.java:73)
at androidx.test.internal.runner.TestLoader.getRunnersFor(TestLoader.java:104)
at androidx.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:789)
at androidx.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:544)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:387)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1879)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance(Native Method)
at androidx.test.ext.junit.runners.AndroidJUnit4.loadRunner(AndroidJUnit4.java:72)
... 16 more
Caused by: org.junit.runners.model.InitializationError
at org.junit.runners.ParentRunner.validate(ParentRunner.java:418)
at org.junit.runners.ParentRunner.<init>(ParentRunner.java:84)
at org.junit.runners.BlockJUnit4ClassRunner.<init>(BlockJUnit4ClassRunner.java:65)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:43)
at androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner.<init>(AndroidJUnit4ClassRunner.java:48)
... 18 more

Tests ran to completion.
like image 334
Alexei Avatar asked Apr 03 '19 13:04

Alexei


3 Answers

I faced this problem in kotlin , used below code and it worked

@RunWith(AndroidJUnit4::class)

@LargeTest

class MainActivityTest {

    private val TAG = "MainActivityTest"

    @get:Rule
    val activityRule = ActivityTestRule(MainActivity::class.java)
}

follow the link below:- https://developer.android.com/training/testing/junit-rules

like image 142
user1035292 Avatar answered Nov 18 '22 09:11

user1035292


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

You can try to put a @Test method and run

@Test
public void signInTest() {


}
like image 45
JackWu Avatar answered Nov 18 '22 10:11

JackWu


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.

like image 8
FarshidABZ Avatar answered Nov 18 '22 09:11

FarshidABZ