1

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.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • 1
    `@RunWith(JUnit4.class)` instead of `@RunWith(AndroidJUnit4.class)` – denys Nov 17 '16 at 12:00
  • it gives me `java.lang.IllegalStateException: No instrumentation registered! Must run under a registering instrumentation.` when doing this, any idea about the kind of this error ?!! – Muhammed Refaat Nov 17 '16 at 13:28
  • can you try google search?! There are a lot of threads with the same problem. – denys Nov 17 '16 at 15:58
  • I tried for sure, but got nothing, everyone of them has his own different case – Muhammed Refaat Nov 20 '16 at 01:55
  • 1
    There was an issue reported with same problem on github android samples, https://github.com/googlesamples/android-testing/issues/58 , just saw you have commented there too ;) – mallaudin Nov 20 '16 at 08:37
  • 1
    The test class, u provided, runs fine. I think the issue is dependencies or other code in your test class. Note: You added`com.android.support.test:runner:0.5` twice – Rowan Jan 25 '17 at 11:43
  • @Rowan thank you for the notice, I would check that. – Muhammed Refaat Jan 25 '17 at 12:06

2 Answers2

0

Have you declared instrumentation in AndroidManifest.xml file?

<instrumentation
    android:name="android.support.test.runner.AndroidJUnitRunner"
    android:targetPackage="com.your.packageundertest" />
denys
  • 6,834
  • 3
  • 37
  • 36
0

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

mark w.
  • 1,047
  • 9
  • 16