Muhammed Refaat
Muhammed Refaat

Reputation: 9103

can't run espresso test

I'm using Espresso to run android UI test, after including all the required dependencies and create the required class test, when running it give me the error:

java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)

and for sure it is a built-in class so I don't have to ability to alter it, even I tried to create a custom one but that led me to other problems that will not be fixed unless removing the extra constructor added in my custom class, which led me at end to the same problem.

my test class:

@RunWith(AndroidJUnit4.class)
public class CreateTest {

    @Rule
    public ActivityTestRule mainActivityTest = new ActivityTestRule<>(MainActivity.class);

    @Before
    public void setUp() throws Exception {
        Context context = InstrumentationRegistry.getContext();
    }

    @Test
    public void clickAdd(){
        Espresso.onView(withId(R.id.button))
                .perform(click());
        intended(hasComponent(new ComponentName(getTargetContext(), SecondActivity.class)));
    }

}

dependencies:

compile 'junit:junit:4.12'

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile ('com.android.support.test:runner:0.5', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
androidTestCompile ('com.android.support.test:rules:0.5', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
compile('com.android.support.test.espresso:espresso-intents:2.2.1', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

p.s I'm so ready to use another testing api if there is one available, but I searched and found nothing.

Upvotes: 1

Views: 2795

Answers (2)

mark w.
mark w.

Reputation: 1107

Have you added your instrumentation runner to build.gradle file as mentioned in the set up instructions here ?

Upvotes: 0

denys
denys

Reputation: 6910

Have you declared instrumentation in AndroidManifest.xml file?

<instrumentation
    android:name="android.support.test.runner.AndroidJUnitRunner"
    android:targetPackage="com.your.packageundertest" />

Upvotes: 0

Related Questions