我正在尝试使用espresso进行活动测试,但我得到NoClassDefFoundError(请参阅下面的堆栈输出)。我尝试了一些我在stackoverflow上看到的东西,但没有一个对我有用
10-04 23:18:28.901 13845-13845/com.example.test E/dalvikvm﹕ Could not find class 'com.example.test.ui.SplashActivity', referenced from method com.example.test.test.ui.SplashActivityTest.<init>
10-04 23:18:28.901 13845-13845/com.example.test W/dalvikvm﹕ VFY: unable to resolve const-class 2480 (Lcom/example/test/ui/SplashActivity;) in Lcom/example/test/test/ui/SplashActivityTest;
10-04 23:18:28.901 13845-13845/com.example.test D/dalvikvm﹕ VFY: replacing opcode 0x1c at 0x0000
10-04 23:18:28.901 13845-13858/com.example.test I/TestRunner﹕ started: warning(junit.framework.TestSuite$1)
10-04 23:18:28.911 13845-13845/com.example.test I/GoogleInstr﹕ Activities that are still in CREATED to PAUSED: 0
10-04 23:18:28.911 13845-13858/com.example.test I/TestRunner﹕ failed: warning(junit.framework.TestSuite$1)
10-04 23:18:28.911 13845-13858/com.example.test I/TestRunner﹕ ----- begin exception -----
10-04 23:18:28.911 13845-13858/com.example.test I/TestRunner﹕ junit.framework.AssertionFailedError: Exception in constructor: testNextActivityWasLaunchedWithIntent (java.lang.NoClassDefFoundError: com.example.test.ui.SplashActivity
at com.example.test.test.ui.SplashActivityTest.<init>(SplashActivityTest.java:21)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at junit.framework.TestSuite.createTest(TestSuite.java:61)
at junit.framework.TestSuite.addTestMethod(TestSuite.java:294)
at junit.framework.TestSuite.addTestsFromTestCase(TestSuite.java:150)
at junit.framework.TestSuite.<init>(TestSuite.java:129)
at junit.runner.BaseTestRunner.getTest(BaseTestRunner.java:118)
at android.test.AndroidTestRunner.getTest(AndroidTestRunner.java:149)
at android.test.AndroidTestRunner.setTestClassName(AndroidTestRunner.java:57)
at android.test.suitebuilder.TestSuiteBuilder.addTestClassByName(TestSuiteBuilder.java:80)
at android.test.InstrumentationTestRunner.parseTestClass(InstrumentationTestRunner.java:443)
at android.test.InstrumentationTestRunner.parseTestClasses(InstrumentationTestRunner.java:424)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:370)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onCreate(GoogleInstrumentationTestRunner.java:114)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4319)
at android.app.ActivityThread.access$1500(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
)
at junit.framework.Assert.fail(Assert.java:50)
at junit.framework.TestSuite$1.runTest(TestSuite.java:97)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at junit.framework.TestResult.runProtected(TestResult.java:133)
at junit.framework.TestResult.run(TestResult.java:118)
at junit.framework.TestCase.run(TestCase.java:124)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:191)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:176)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:554)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onStart(GoogleInstrumentationTestRunner.java:167)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
我的构建脚本:
buildscript {
dependencies {
repositories {
mavenCentral()
}
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'com.github.jcandksolutions.gradle:android-unit-test:1.6.2'
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 20
buildToolsVersion "20.0.0"
defaultConfig {
applicationId "com.example.test"
minSdkVersion 15
targetSdkVersion 20
versionCode 1
versionName "1.0"
applicationId "com.example.test"
testApplicationId "com.example.test"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
testHandleProfiling true
testFunctionalTest true
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
testCoverageEnabled = true
}
}
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
exclude 'META-INF/ASL2.0'
exclude 'LICENSE.txt'
}
lintOptions {
abortOnError false
ignoreWarnings true
}
}
apply plugin: 'android-unit-test'
apply plugin: 'jacoco'
jacoco {
toolVersion = "0.7.1.201405082137"
}
def coverageSourceDirs = [
'../app/src/main/java'
]
task jacocoTestReport(type: JacocoReport, dependsOn: "testDebug") {
group = "Reporting"
description = "Generate Jacoco coverage reports"
classDirectories = fileTree(
dir: '../app/build/intermediates/classes/debug',
excludes: ['**/R.class',
'**/R$*.class',
'**/*$ViewInjector*.*',
'**/BuildConfig.*',
'**/Manifest*.*']
)
additionalSourceDirs = files(coverageSourceDirs)
sourceDirectories = files(coverageSourceDirs)
executionData = files('../app/build/jacoco/testDebug.exec')
reports {
xml.enabled = true
html.enabled = true
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.google.android.gms:play-services:5.2.08'
compile 'com.android.support:appcompat-v7:20.+'
androidTestCompile ('com.jakewharton.espresso:espresso:1.1-r3') {
exclude group: 'org.hamcrest', module: 'hamcrest-integration'
}
androidTestCompile ('com.jakewharton.espresso:espresso-support-v4:1.1-r3') {
exclude group:'com.android.support'
exclude group: 'org.hamcrest', module: 'hamcrest-integration'
}
}
答案 0 :(得分:1)
你有testApplicationId "com.example.test"
是有意的,不应该有&#34;测试&#34;后缀:
totestApplicationId "com.example.test.test"