4

我正在尝试运行 Android espresso 测试,但他们一直以 java.lang.NoClassDefFoundError: com.blokura.testexample.app.MainActivity 失败

我正在使用 Jake Wharton 的双份浓缩咖啡插件。

我尝试了在github repodeckard上看到的 Jake 的设置,但没有运气。

我不知道我错过了什么。

这是项目结构 项目结构

这是 build.gradle 文件源代码:

    apply plugin: 'android'

android {
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
    }

    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
    sourceSets {
        androidTest {
            setRoot('src/test')
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r3'
    androidTestCompile 'com.jakewharton.espresso:espresso-support-v4:1.1-r3'
}

EspressoTest.java

package com.blokura.testexample.app;

import android.test.ActivityInstrumentationTestCase2;
import android.test.suitebuilder.annotation.LargeTest;

import com.blokura.testexample.app.MainActivity;

import static com.google.android.apps.common.testing.ui.espresso.Espresso.onView;
import static com.google.android.apps.common.testing.ui.espresso.assertion.ViewAssertions.matches;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withId;
import static com.google.android.apps.common.testing.ui.espresso.matcher.ViewMatchers.withText;
import java.lang.Exception;
import java.lang.Override;

@LargeTest
public class EspressoTest extends ActivityInstrumentationTestCase2<MainActivity>{

    @SuppressWarnings("deprecation")
    public EspressoTest() {
        super("com.blokura.testexample.app", MainActivity.class);
    }

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        getActivity();
    }

    public void testCheckText() {
        onView(withId(R.id.text))
                .check(matches(withText("Hello World!")));
    }
}

MainActivity.java

package com.blokura.testexample.app;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

完整的日志

java.lang.RuntimeException: Exception during suite construction
at android.test.suitebuilder.TestSuiteBuilder$FailedToCreateTests.testSuiteConstructionFailed(TestSuiteBuilder.java:238)
at java.lang.reflect.Method.invokeNative(Native Method)
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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.test.suitebuilder.TestMethod.instantiateTest(TestMethod.java:87)
at android.test.suitebuilder.TestMethod.createTest(TestMethod.java:73)
at android.test.suitebuilder.TestSuiteBuilder.addTest(TestSuiteBuilder.java:262)
at android.test.suitebuilder.TestSuiteBuilder.build(TestSuiteBuilder.java:184)
at android.test.InstrumentationTestRunner.onCreate(InstrumentationTestRunner.java:379)
at com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner.onCreate(GoogleInstrumentationTestRunner.java:114)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4335)
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:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: com.blokura.testexample.app.MainActivity
at com.blokura.testexample.app.EspressoTest.<init>(EspressoTest.java:20)
... 19 more
4

3 回答 3

5

呃,我忘了在 gradle 文件中排除支持库:(

这是正确的 build.gradle 文件

apply plugin: 'android'

android {
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
    }

    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:19.+'
    androidTestCompile 'com.jakewharton.espresso:espresso:1.1-r3'
    androidTestCompile ('com.jakewharton.espresso:espresso-support-v4:1.1-r3') {
        exclude group:'com.android.support', module:'support-v4'
    }
}
于 2014-05-12T20:43:10.447 回答
1

首先,将test文件夹更改为androidTest. 其次,检查Espresso的最新版本。例如,我在一个项目中使用了这个 gradle 设置,它对我有用:

***
android {
  compileSdkVersion 23
  buildToolsVersion "23.0.1"        
  defaultConfig {
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
     ***
  }
  ***
}    
dependencies {
    androidTestCompile 'com.android.support.test:runner:0.4'
    androidTestCompile 'com.android.support.test:rules:0.4'
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
}
于 2015-03-16T13:36:18.730 回答
0

Gradle 在 Android Studio 中的最新约定是将您的测试目录称为“ androidTest ”。然后在您的 build.gradle 文件中,您需要输入:

    androidTest.setRoot('androidTest')
    androidTest.java.srcDirs = ['androidTest/java']
于 2014-05-14T17:13:23.567 回答