浓缩咖啡测试 class 未找到

Espresso test class not found

我创建了一个基本的 Espresso 测试 class,如下所示:

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

问题是,当我尝试 运行 此测试时,我收到以下错误消息:

Class not found: "com.name.app.activities.ConfigurationsTest"

我遵循了这个答案:Android Espresso test: Class not found: ...Empty test suite 并查看了我的 运行 配置。看来我的测试 运行 作为一个单元测试,即使它是一个仪器测试。

当我删除单元测试配置时会出现另一个问题,这些配置是在我尝试 运行 我的插桩测试时创建的,并尝试为我的测试创建插桩测试 运行 配置 class:向导不允许我 select 我的仪器测试 class 包含上面的测试作为测试 class。 The problem is visualised here.

我也看了这个答案:TestCase class not found by Android Studio,确认我在main/java和androidTest/java的目录结构是一样的

此外,当我按照这个问题所做的操作时:Android Espresso: "No test were found" , "Process crashed" 并为包含我的测试 class 和 [=44= 的整个包创建一个 运行 配置] 那,我得到:

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

我怀疑这与我的导入或依赖项有关。两者都是:

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'
}

感谢任何帮助。谢谢!

规则应该是这样的

@Rule
public ActivityTestRule<ConfigurationsActivity> mConfigsTestRule =
            new ActivityTestRule<>(ConfigurationsActivity.class);

测试 class 必须是 public。这就是它不起作用的原因。