我创建了一个基本的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,并查看了我的跑步配置。看来我的测试是作为单元测试运行的,即使它是经过测试的。
当我删除单元测试配置时,会出现另一个问题,这些配置是在我尝试运行检测的测试时创建的,并尝试为我的测试类创建检测的测试运行配置:向导不允许我选择检测的配置包含上述测试的测试类为测试类。 The problem is visualised here.
我也查看了以下答案: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'
}
任何帮助表示赞赏。谢谢!
答案 0 :(得分:0)
规则应该是这样
@Rule
public ActivityTestRule<ConfigurationsActivity> mConfigsTestRule =
new ActivityTestRule<>(ConfigurationsActivity.class);
答案 1 :(得分:0)
测试班必须是公开的。这就是为什么它不起作用。