java.lang.Exception:自定义运行器类AndroidJUnit4应该有一个带有签名AndroidJUnit4的公共构造函数(Class testClass)
作者:互联网
gradle看起来像:
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "com.google.developer.taskmaker"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
configurations.all {
resolutionStrategy {
force 'com.android.support:support-annotations:25.2.0'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.2.0'
compile 'com.android.support:recyclerview-v7:25.2.0'
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:preference-v7:25.2.0'
debugCompile 'im.dino:dbinspector:3.4.1@aar'
// Android JUnit Runner
compile 'com.google.android.gms:play-services-appindexing:8.4.0'
// Android runner and rules support
// add this for intent mocking support
androidTestCompile 'com.android.support.test.espresso:espresso-intents:2.2.2'
// add this for webview testing support
androidTestCompile 'com.android.support.test.espresso:espresso-web:2.2.2'
compile 'com.android.support.test:runner:0.5'
compile 'com.android.support.test:rules:0.5'
compile 'com.android.support.test.espresso:espresso-core:2.2.2'
}
单元测试用例看起来像
@RunWith(AndroidJUnit4.class)
public class TestClass {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);
@Test
public void buttonClick(){
onView(withId(R.id.fab)).perform(click()).check(matches(isDisplayed()));
}
}
错误消息如下所示:
java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)
at org.junit.runners.model.InitializationError.<init>(InitializationError.java:38)
at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:111)
at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:101)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:87)
at com.intellij.junit4.JUnit46ClassesRequestBuilder.collectWrappedRunners(JUnit46ClassesRequestBuilder.java:90)
at com.intellij.junit4.JUnit46ClassesRequestBuilder.getClassesRequest(JUnit46ClassesRequestBuilder.java:51)
at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:91)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:95)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Process finished with exit code -1
我已经检查了其他答案,但没有得到解决方案.请告诉我哪里出错了.运行此代码时,我收到错误
解决方法:
原因1:将测试放在错误的文件夹中
通过在Android Studio中使用默认模板创建一个新的Application并将自动生成的ExampleInstrumentedTest从androidTest文件夹复制到test文件夹,可以复制此错误:
这是错误的样子:
请注意,为了复制问题,您还必须错误地将依赖项添加到模块级build.gradle:
原因2:测试配置错误
本地单元测试和仪表测试具有不同的运行配置.如果单击下面的编辑配置:
您应该在Android仪器测试下看到您的仪器化测试,并在Android JUnit下看到您的本地单元测试,如下图所示:
对于Android Instrumented Tests,配置应如下所示:
对于Android JUnit:
运行测试时,Android Studio将为您创建运行配置.如果运行配置的类别错误,请检查您的测试和androidTest文件夹是否正确,然后您可以从编辑配置中删除运行配置并再次运行测试.如果您已正确设置,Android Studio将使用正确类型的运行配置.
说明
Android中有两种类型的测试:
>仪器化测试
>本地单元测试
仪器化测试
仪表测试是旨在在手机或仿真器上运行的测试.这些是需要访问Android库的全功能部分的测试(例如真实的Context).这些需要进入androidTest文件夹,并且这些测试的依赖项(例如,Espresso,com.android.support.test.rules:0.5)将在build.gradle中以androidTestCompile为前缀.
这是一个示例检测测试:
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.assertEquals;
/**
* Instrumentation test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void useAppContext() throws Exception {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();
assertEquals("com.gyaltsab.myapplication", appContext.getPackageName());
}
}
本地单元测试
本地单元测试是可以在IDE中运行的测试.它们通常不依赖于标准JVM上不可用的Android库的任何部分(例如,它们不依赖于上下文).这些依赖项将在build.gradle的testCompile部分中进行.
这是一个示例单元测试:
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Example local unit test, which will execute on the development machine (host).
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
public class ExampleUnitTest {
@Test
public void addition_isCorrect() throws Exception {
assertEquals(4, 2 + 2);
}
}
请注意,本地单元测试不需要在类声明中使用@RunWith(AndroidJUnit4.class)注释.有关更完整的说明,请参阅the official docs.
标签:android,android-espresso 来源: https://codeday.me/bug/20191006/1860302.html