Espresso test class not found

Issue

I have created a basic Espresso test class that looks like the following:

@LargeTest
@RunWith(AndroidJUnit4.class)
class ConfigurationsActivityTest {
    @Rule
    public ActivityTestRule<ConfigurationsActivity> mConfigsTestRule 
            new ActivityTestRule<>(ConfigurationsActivity.class);

    @Test
    public void isInView() {
        onView(withId(R.id.config_recyclerview)).check(matches(isDisplayed()));
    }
}

The problem is, when I try to run this test I get the following error message:

Class not found: “com.name.app.activities.ConfigurationsTest”

I have followed this answer: Android Espresso test: Class not found: ‚ĶEmpty test suite and taken a look at my run configurations. It seems that my test is being run as an unit test even though it’s an instrumented one.

Another problem arises when I delete the unit test configurations, that were created when I tried to run my instrumented test, and try to create an instrumented test run configuration for my test class: the wizard does not allow me to select my instrumented test class containing the test above to be the test class. The problem is visualised here.

I have also taken a look at this answer: TestCase class not found by Android Studio and confirmed that my directory structure in main/java and androidTest/java are the same.

In addition, when I follow what was done in this question: Android Espresso: “No test were found” , “Process crashed” and create a run configuration for the whole package that contains my test class and run that, I get:

java.lang.RuntimeException: Delegate runner ‘androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner’ for AndroidJUnit4 could not be loaded.

I am suspecting that it’s something to do with my imports or dependencies. Here are both:

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;


dependencies {
    implementation 'com.google.code.gson:gson:2.8.5'
    implementation 'com.github.bumptech.glide:glide:3.7.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    implementation 'com.google.android.material:material:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
    implementation 'androidx.preference:preference:1.1.1'
    testImplementation 'junit:junit:4.13'
    testImplementation "org.mockito:mockito-core:3.3.1"
    androidTestImplementation 'androidx.test:rules:1.2.0'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.2.0'
}

Any help appreciated. Thanks!

Solution

The test class has to be public. That’s why it wasn’t working.

Answered By – arttuOll

Leave a Comment