gpt4 book ai didi

android - java.lang.Exception : Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)

转载 作者:IT老高 更新时间:2023-10-28 23:03:22 34 4
gpt4 key购买 nike

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 中使用默认模板创建一个新应用程序并复制自动生成的 ExampleInstrumentedTest 来复制此错误。来自 androidTest文件夹到 test文件夹:

incorrectly copying an instrumented test into the test folder

这是错误的样子:

the OPs error java.lang.Exception: Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass)

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

dependencies in the wrong place

原因2:测试配置错误

本地单元测试和插装测试具有不同的运行配置。如果您单击 Edit Configurations,如下所示:

test configuration

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

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

test config for Android Instrumented tests

对于 Android JUnit:

test config for Android JUnit tests

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

说明

Android 中有两种类型的测试:

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

仪器测试

仪器测试是设计用于在手机模拟器上运行的测试。在这些测试中,您需要访问 Android 库的完整功能部分(例如真实的 Context)。这些需要进入androidTest这些测试的文件夹和依赖项(例如,Espresso、com.android.support.test.rules:0.5)将以 androidTestCompile 为前缀在您的build.gradle .

这是一个仪器测试示例:

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 )。这些依赖项位于 testCompile您的 build.gradle 的一部分.

这是一个示例单元测试:

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 - java.lang.Exception : Custom runner class AndroidJUnit4 should have a public constructor with signature AndroidJUnit4(Class testClass),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43171449/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com