0

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

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

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

找不到类:“com.name.app.activities.ConfigurationsTest”

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

当我删除单元测试配置时出现另一个问题,这些配置是在我尝试运行我的检测测试时创建的,并尝试为我的测试类创建检测测试运行配置:向导不允许我选择我的检测测试类包含上面的测试是测试类。问题在这里可视化。

我也看过这个答案:TestCase class not found by Android Studio并确认我在 main/java 和 androidTest/java 中的目录结构是相同的。

此外,当我按照这个问题所做的事情:Android Espresso: "No test were found" , "Process crashed"并为包含我的测试类的整个包创建运行配置并运行它时,我得到:

java.lang.RuntimeException:无法加载 AndroidJUnit4 的委托运行程序“androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner”。

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

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

任何帮助表示赞赏。谢谢!

4

2 回答 2

1

规则应该是这样的

@Rule
public ActivityTestRule<ConfigurationsActivity> mConfigsTestRule =
            new ActivityTestRule<>(ConfigurationsActivity.class);
于 2020-07-09T21:00:15.977 回答
0

测试类必须是公开的。这就是为什么它不起作用。

于 2020-07-10T16:19:23.850 回答