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.
总结以上是内存溢出为你收集整理的java.lang.Exception:自定义运行器类AndroidJUnit4应该有一个带有签名AndroidJUnit4的公共构造函数(Class testClass)全部内容,希望文章能够帮你解决java.lang.Exception:自定义运行器类AndroidJUnit4应该有一个带有签名AndroidJUnit4的公共构造函数(Class testClass)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)