android – junit.framework.AssertionFailedError:构造函数中的异常:(java.lang.NoClassDefFoundError)

前端之家收集整理的这篇文章主要介绍了android – junit.framework.AssertionFailedError:构造函数中的异常:(java.lang.NoClassDefFoundError)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试将espresso2实现到我的项目中,我无法确定测试中的appcompat库依赖性的问题.当我使用Activity,它工作.当我使用ActionBarActivity或FragmentActivity时,我得到这个错误
junit.framework.AssertionFailedError: Exception in constructor: testCheckText (java.lang.NoClassDefFoundError: ait.common.testunittest.app.MainActivity2
at ait.common.testunittest.app.test.MainActivity2Test.<init>(MainActivity2Test.java:18)
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 android.support.test.internal.runner.junit3.NonLeakyTestSuite.<init>(NonLeakyTestSuite.java:34)
at android.support.test.internal.runner.junit3.AndroidTestSuite.<init>(AndroidTestSuite.java:43)
at android.support.test.internal.runner.junit3.AndroidJUnit3Builder.runnerForClass(AndroidJUnit3Builder.java:50)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runner.Computer.getRunner(Computer.java:38)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:98)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
at org.junit.runners.Suite.<init>(Suite.java:79)
at org.junit.runner.Computer.getSuite(Computer.java:26)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:598)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:578)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:542)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
)
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 android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)



12-21 16:24:57.187: W/dalvikvm(8197): Class resolved by unexpected DEX: Landroid/support/v7/app/ActionBarActivity;(0xa5047ce0):0x95
junit.framework.AssertionFailedError: Exception in constructor: testCheckText (java.lang.NoClassDefFoundError: ait.common.testunittest.app.MainActivity2
at ait.common.testunittest.app.test.MainActivity2Test.<init>(MainActivity2Test.java:18)
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 android.support.test.internal.runner.junit3.NonLeakyTestSuite.<init>(NonLeakyTestSuite.java:34)
at android.support.test.internal.runner.junit3.AndroidTestSuite.<init>(AndroidTestSuite.java:43)
at android.support.test.internal.runner.junit3.AndroidJUnit3Builder.runnerForClass(AndroidJUnit3Builder.java:50)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runner.Computer.getRunner(Computer.java:38)
at org.junit.runner.Computer$1.runnerForClass(Computer.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:98)
at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84)
at org.junit.runners.Suite.<init>(Suite.java:79)
at org.junit.runner.Computer.getSuite(Computer.java:26)
at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:598)
at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:578)
at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:542)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
)
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 android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:24)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)
junit.framework.AssertionFailedError: Exception in constructor: testCheckText (java.lang.NoClassDefFoundError: ait.common.testunittest.app.MainActivity2 at ait.common.testunittest.app.test.MainActivity2Test.<init>(MainActivity2Test.java:18) 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 android.support.test.internal.runner.junit3.NonLeakyTestSuite.<init>(NonLeakyTestSuite.java:34) at android.support.test.internal.runner.junit3.AndroidTestSuite.<init>(AndroidTestSuite.java:43) at android.support.test.internal.runner.junit3.AndroidJUnit3Builder.runnerForClass(AndroidJUnit3Builder.java:50) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29) at org.junit.runner.Computer.getRunner(Computer.java:38) at org.junit.runner.Computer$1.runnerForClass(Computer.java:29) at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57) at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:98) at org.junit.runners.model.RunnerBuilder.runners(RunnerBuilder.java:84) at org.junit.runners.Suite.<init>(Suite.java:79) at org.junit.runner.Computer.getSuite(Computer.java:26) at android.support.test.internal.runner.TestRequestBuilder.classes(TestRequestBuilder.java:598) at android.support.test.internal.runner.TestRequestBuilder.build(TestRequestBuilder.java:578) at android.support.test.runner.AndroidJUnitRunner.buildRequest(AndroidJUnitRunner.java:542) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:269) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701) ) 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 android.support.test.internal.runner.junit3.DelegatingTestResult.runProtected(DelegatingTestResult.java:90) at junit.framework.TestResult.run(TestResult.java:118) at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:49) at junit.framework.TestCase.run(TestCase.java:124) at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63) at junit.framework.TestSuite.runTest(TestSuite.java:243) at junit.framework.TestSuite.run(TestSuite.java:238) at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103) at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:63) at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:90) at org.junit.runners.Suite.runChild(Suite.java:128) at org.junit.runners.Suite.runChild(Suite.java:24) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) at org.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at org.junit.runner.JUnitCore.run(JUnitCore.java:136) at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270) at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1701)0 ref [Landroid/support/v4/app/FragmentActivity;] Landroid/support/v4/app/FragmentActivity;(0xa5047ce0):0x9523e000
12-21 16:24:57.187: W/dalvikvm(8197): (Landroid/support/v7/app/ActionBarActivity; had used a different Landroid/support/v4/app/FragmentActivity; during pre-verification)
12-21 16:24:57.187: W/dalvikvm(8197): Unable to resolve superclass of Landroid/support/v7/app/ActionBarActivity; (285)
12-21 16:24:57.187: W/dalvikvm(8197): Link of class 'Landroid/support/v7/app/ActionBarActivity;' Failed
12-21 16:24:57.187: W/dalvikvm(8197): Unable to resolve superclass of Lait/common/testunittest/app/MainActivity2; (1026)
12-21 16:24:57.187: W/dalvikvm(8197): Link of class 'Lait/common/testunittest/app/MainActivity2;' Failed
12-21 16:24:57.191: E/dalvikvm(8197): Could not find class 'ait.common.testunittest.app.MainActivity2',referenced from method ait.common.testunittest.app.test.MainActivity2Test.<init>
12-21 16:24:57.191: W/dalvikvm(8197): VFY: unable to resolve const-class 6 (Lait/common/testunittest/app/MainActivity2;) in Lait/common/testunittest/app/test/MainActivity2Test;
package ait.common.testunittest.app; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; public class MainActivity2 extends ActionBarActivity { Fragment fragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:0.14.2' } } apply plugin: 'com.android.application' repositories { jcenter() } android { compileSdkVersion 21 buildToolsVersion "21.1.1" defaultConfig { applicationId "ait.common.testunittest.app" testApplicationId "ait.common.testunittest.app.test" minSdkVersion 15 targetSdkVersion 21 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro' } } packagingOptions { exclude 'Meta-INF/DEPENDENCIES.txt' exclude 'Meta-INF/LICENSE.txt' exclude 'Meta-INF/NOTICE.txt' exclude 'Meta-INF/NOTICE' exclude 'Meta-INF/LICENSE' exclude 'Meta-INF/DEPENDENCIES' exclude 'Meta-INF/notice.txt' exclude 'Meta-INF/license.txt' exclude 'Meta-INF/dependencies.txt' exclude 'Meta-INF/LGPL2.1' exclude 'Meta-INF/ASL2.0' exclude 'Meta-INF/ASL2.0' exclude 'LICENSE.txt' } lintOptions { abortOnError false } sourceSets { androidTest { setRoot('src/test') } } } dependencies { compile fileTree(include: ['*.jar'],dir: 'libs') compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.android.support:support-v4:21.0.3' // Espresso androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0' androidTestCompile 'com.android.support.test:testing-support-lib:0.1' androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0' }package ait.common.testunittest.app.test; import ait.common.testunittest.app.MainActivity; import ait.common.testunittest.app.R; import android.support.test.espresso.Espresso; import android.support.test.espresso.assertion.ViewAssertions; import android.support.test.espresso.matcher.ViewMatchers; import android.test.ActivityInstrumentationTestCase2; public class MainActivityTest extends ActivityInstrumentationTestCase2<MainActivity2> { public MainActivityTest() { // This constructor was deprecated - but we want to support lower API levels. super(MainActivity2.class); } @Override public void setUp() throws Exception { super.setUp(); // Espresso will not launch our activity for us,we must launch it via getActivity(). getActivity(); } public void testCheckText() { Espresso.onView(ViewMatchers.withId(R.id.test)) .check(ViewAssertions.matches(ViewMatchers.withText(R.string.hello_world))); } }

解决方法

我可以通过排除我在应用程序的依赖关系中使用的支持内容来获得与Gradle的配合:
//Testing
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
    exclude group: 'com.android.support',module: 'appcompat'
    exclude group: 'com.android.support',module: 'support-v4'
    exclude module: 'recyclerview-v7'
}
原文链接:https://www.f2er.com/android/311289.html

猜你在找的Android相关文章