Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

junit.framework.AssertionFailedError: Exception in constructor: (java.lang.NoClassDefFoundError)

I am trying to implement espresso2 into my project and I can't figure out problem with appcompat library dependency in test. When I used Activity, it worked. When I am used ActionBarActivity or FragmentActivity, then I got this error:


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)));
        }
    }
like image 217
bmoss Avatar asked Dec 21 '14 17:12

bmoss


2 Answers

I was able to get this to work with Gradle by excluding support stuff I was using in my app's dependencies:

//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'
}
like image 157
DesignatedNerd Avatar answered Oct 18 '22 01:10

DesignatedNerd


I had the same problem and noticed when taking away

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

The test ran fine. I really want the new RecyclerViewActions so I tried downloading the espresso-contrib-2.0 jar separately. This strangely enough fixed the problem for me. Here's a direct link to the repo https://code.google.com/p/android-test-kit/source/browse/#git%2Fbin%2Fespresso2.0

like image 1
MadTom Avatar answered Oct 17 '22 23:10

MadTom