java.lang.Exception:自定义运行器类AndroidJUnit4应该具有带有签名AndroidJUnit4(Class testClass)的公共构造函数 [英] java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)

查看:100
本文介绍了java.lang.Exception:自定义运行器类AndroidJUnit4应该具有带有签名AndroidJUnit4(Class testClass)的公共构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

gradle看起来像:

gradle looks like:

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

我已经检查了其他答案,但没有找到解决方法.请告诉哪里出了问题.运行此代码时出现错误

I have already check other answers but not getting solution.Please tell where thing goes wrong. When running this code I am getting error

推荐答案

原因1:将测试放在错误的文件夹中

可以通过以下方法来复制此错误:在Android Studio中使用默认模板制作一个新的应用程序,并将自动生成的ExampleInstrumentedTestandroidTest文件夹复制到test文件夹:

Cause 1: placing tests in the wrong folder

This error can be replicated by making a new Application with the default template in Android Studio and copying the auto-generated ExampleInstrumentedTest from the androidTest folder to the test folder:

这是错误的样子:

请注意,为了复制问题,您还必须错误地将依赖项添加到模块级build.gradle:

Note that in order to replicate the problem you would also have to incorrectly add dependencies to the module-level build.gradle:

本地单元测试和仪器测试具有不同的运行配置.如果您点击 Edit Configurations ,如下所示:

Local unit tests and instrumented tests have different Run configurations. If you click Edit Configurations like below:

您应该在Android Instrumented Tests下看到仪器化测试,而在Android JUnit下看到本地单元测试,如下图所示:

You should see your instrumented tests under Android Instrumented Tests and your local unit tests under the Android JUnit like in the illustrations below:

对于Android Instrumented测试,配置应如下所示:

For Android Instrumented Tests the config should look something like below:

对于Android JUnit:

For Android JUnit:

运行测试时,Android Studio会为您创建一个运行配置.如果运行配置属于错误的类别,请检查您的testandroidTest文件夹是否正确,然后可以从Edit configurations中删除运行配置,然后再次运行测试.如果设置正确,则Android Studio这次将使用正确的运行配置类型.

When you run a test, Android Studio will create a run config for you. If the run config is in the wrong category, check you have your test and androidTest folders correct and then you can delete the run config from the Edit configurations and run the test again. If you have set up correctly, Android Studio will use the correct type of run configuration this time.

Android中有两种测试类型:

There are two types of tests in Android:

  1. 仪器测试
  2. 本地单元测试

仪器测试

仪器测试是旨在在手机模拟器上运行的测试.在这些测试中,您需要访问Android库的全功能部分(例如,真实的Context).这些需要放在androidTest文件夹中,并且这些测试的依赖项(例如,Espresso,com.android.support.test.rules:0.5)将在您的build.gradle中以androidTestCompile为前缀.

Instrumented tests

Instrumented tests are tests that are designed to run on a handset or emulator. These are tests where you need access to a fully-functional part of the Android library (like a real Context for example). These need to go in the androidTest folder and dependencies for these tests (e.g., Espresso, com.android.support.test.rules:0.5) will be prefixed with androidTestCompile in your build.gradle.

这是仪器化测试的示例:

Here is an example instrumented test:

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库的任何部分(例如,它们不依赖于Context).这些依赖项位于build.gradletestCompile部分.

Local unit tests

Local unit tests are tests that you can run in your IDE. They normally don't depend on any part of the Android library not available on a standard JVM (e.g., they won't depend on Context). The dependencies for these go in the testCompile part of your build.gradle.

这是一个示例单元测试:

Here is an example unit test:

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)批注.有关更完整的说明,请参见官方文档.

Note that local unit tests do not need the @RunWith(AndroidJUnit4.class) annotation in the class declaration. Please see the official docs for a more complete explanation.

这篇关于java.lang.Exception:自定义运行器类AndroidJUnit4应该具有带有签名AndroidJUnit4(Class testClass)的公共构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆