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

时间:2014-12-21 17:31:52

标签: android unit-testing android-appcompat android-espresso

我正在尝试将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):0x95060000 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)));
        }
    }

2 个答案:

答案 0 :(得分:23)

通过排除我在我的应用dependencies中使用的支持内容,我能够将其与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'
}

答案 1 :(得分:1)

我有同样的问题并且在带走时注意到了

androidTestCompile 'com.android.support.test.espresso:espresso-contrib:2.0'

测试运行良好。我真的想要新的RecyclerViewActions,所以我尝试分别下载espresso-contrib-2.0 jar。这奇怪地解决了我的问题。这是与回购https://code.google.com/p/android-test-kit/source/browse/#git%2Fbin%2Fespresso2.0

的直接链接