我可以在Espresso中扩展自定义应用程序吗?

时间:2015-04-20 14:05:16

标签: android dagger android-espresso

我试图在Espresso仪器测试中设置Dagger,以模拟对外部资源的调用(在这种情况下为RESTful服务)。我在Robolectric中为我的单元测试所遵循的模式是扩展我的生产Application类并使用将返回模拟的测试模块覆盖Dagger模块。我试图在这里做同样的事情,但是当我尝试将应用程序转换为我的自定义应用程序时,我在Espresso测试中得到了ClassCastException。

这是我到目前为止的设置:

生产

在app / src / main / java / com / mypackage / injection下我有:

MyCustomApplication

package com.mypackage.injection;

import android.app.Application;

import java.util.ArrayList;
import java.util.List;

import dagger.ObjectGraph;

public class MyCustomApplication extends Application {

    protected ObjectGraph graph;

    @Override
    public void onCreate() {
        super.onCreate();

        graph = ObjectGraph.create(getModules().toArray());
    }

    protected List<Object> getModules() {
        List<Object> modules = new ArrayList<Object>();
        modules.add(new AndroidModule(this));
        modules.add(new RemoteResourcesModule(this));
        modules.add(new MyCustomModule());

        return modules;
    }

    public void inject(Object object) {
        graph.inject(object);
    }
}

我以下列方式使用:

BaseActivity

package com.mypackage.injection.views;

import android.app.Activity;
import android.os.Bundle;

import com.mypackage.injection.MyCustomApplication;

public abstract class MyCustomBaseActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ((MyCustomApplication)getApplication()).inject(this);
    }

}

受测试的活动

package com.mypackage.views.mydomain;
// imports snipped for bevity

public class MyActivity extends MyBaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //snip
    }
}

Espresso设置

在app / src / androidTest / java / com / mypackage / injection下我有:

MyCustomEspressoApplication

package com.mypackage.injection;

import java.util.ArrayList;
import java.util.List;

import dagger.ObjectGraph;

public class MyCustomEspressoApplication extends MyCustomApplication {

    private AndroidModule androidModule;
    private MyCustomModule myCustomModule;
    private EspressoRemoteResourcesModule espressoRemoteResourcesModule;

    @Override
    public void onCreate() {
        super.onCreate();

        graph = ObjectGraph.create(getModules().toArray());
    }

    protected List<Object> getModules() {
        List<Object> modules = new ArrayList<Object>();
        modules.add(getAndroidModule());
        modules.add(getEspressoRemoteResourcesModule());
        modules.add(getMyCustomModule());

        return modules;
    }

    public void inject(Object object) {
        graph.inject(object);
    }

    public AndroidModule getAndroidModule() {
        if (this.androidModule == null) {
            this.androidModule = new AndroidModule(this);
        }

        return this.androidModule;
    }

    public MyCustomModule getMyCustomModule() {
        if (this.myCustomModule == null) {
            this.myCustomModule = new MyCustomModule();
        }

        return this.myCustomModule;
    }

    public EspressoRemoteResourcesModule getEspressoRemoteResourcesModule() {
        if (this.espressoRemoteResourcesModule == null) {
            this.espressoRemoteResourcesModule = new EspressoRemoteResourcesModule();
        }

        return this.espressoRemoteResourcesModule;
    }
}

我的Espresso测试,在app / src / androidTest / com / mypackage / espresso下:

package com.mypackage.espresso;

// imports snipped for brevity

@RunWith(AndroidJUnit4.class)
@LargeTest
public class MyActivityTest extends   ActivityInstrumentationTestCase2<MyActivity>{

    private MyActivity myActivity;

    public MyActivityTest() {
        super(MyActivity.class);
    }

    @Before
    public void setUp() throws Exception {
        super.setUp();
        injectInstrumentation(InstrumentationRegistry.getInstrumentation());
        myActivity = getActivity();
    }

    @After
    public void tearDown() throws Exception {
        super.tearDown();
    }

     @Test
     public void testWhenTheActionBarButtonIsPressedThenThePlacesAreListed() {
         //The next line is where the runtime exception occurs.
         MyCustomEspressoApplication app = (MyCustomEspressoApplication)getInstrumentation().getTargetContext().getApplicationContext();
        //I've also tried getActivity().getApplication() and 
        // getActivity.getApplicationContext() with the same results
        //snip
     }
}

我的AndroidManifest.xml

(我之前在自定义应用程序类中看到过很多关于ClassCastException的答案,而且大多数都指向应用程序节点上缺少&#34; android:name&#34;属性。我&#39;我在这里粘贴这个以表明,据我所知,情况并非如此。)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.mypackage">   
    <!-- snip --> 
    <application
        android:name=".injection.MyCustomApplication"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
    <!-- snip -->
    </application>
<!-- snip -->
</manifest>

的build.gradle

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
}

apply plugin: 'com.android.application'
apply plugin: 'idea'

android {
    testOptions {
        unitTests.returnDefaultValues = true
    }
    lintOptions {
        abortOnError false
    }
   packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
    }
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.mypackage"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

idea {
    module {
        testOutputDir = file('build/test-classes/debug')
    }
}

dependencies {
    compile project(':swipeablecardview')

    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:support-annotations:21.0.3'
    compile 'com.android.support:appcompat-v7:21.0.3'
    compile 'com.squareup:javawriter:2.5.0'
    compile ('com.squareup.dagger:dagger:1.2.2') {
        exclude module: 'javawriter'
    }
    compile ('com.squareup.dagger:dagger-compiler:1.2.2') {
        exclude module: 'javawriter'
    }
    compile 'com.melnykov:floatingactionbutton:1.1.0'
    compile 'com.android.support:cardview-v7:21.0.+'
    compile 'com.android.support:recyclerview-v7:21.0.+'
    //    compile 'se.walkercrou:google-places-api-java:2.1.0'
    compile 'org.apache.httpcomponents:httpclient-android:4.3.5.1'
    compile 'commons-io:commons-io:1.3.2'
    testCompile 'org.hamcrest:hamcrest-integration:1.3'
    testCompile 'org.hamcrest:hamcrest-core:1.3'
    testCompile 'org.hamcrest:hamcrest-library:1.3'
    testCompile('junit:junit:4.12')
    testCompile 'org.mockito:mockito-core:1.+'
    testCompile('org.robolectric:robolectric:3.0-SNAPSHOT')
    testCompile('org.robolectric:shadows-support-v4:3.0-SNAPSHOT')
    androidTestCompile 'org.mockito:mockito-core:1.+'
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
        exclude group: 'javax.inject'
        exclude module: 'javawriter'
    }
    androidTestCompile('com.android.support.test:testing-support-lib:0.1')
}

stacktrace:

  

java.lang.ClassCastException:   com.mypackage.injection.MyCustomApplication无法强制转换为   com.mypackage.injection.MyCustomEspressoApplication at   com.mypackage.espresso.MyActivityTest.testWhenTheActionBarButtonIsPressedThenThePlacesAreListed(MyActivityTest.java:107)   在java.lang.reflect.Method.invokeNative(Native Method)at   java.lang.reflect.Method.invoke(Method.java:511)at   org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:45)   在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)   在   org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)   在   org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)   在   org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)   在   org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)   在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)at   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)   在   org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)   在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 at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:300)at 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 at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:50)at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:222)at at   org.junit.runners.ParentRunner.run(ParentRunner.java:300)at at   org.junit.runner.JUnitCore.run(JUnitCore.java:157)at   org.junit.runner.JUnitCore.run(JUnitCore.java:136)at at   android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:270)   在   android.app.Instrumentation $ InstrumentationThread.run(Instrumentation.java:1551)

我已经阅读了Espresso和Dagger文档并搜索了Github上的问题但无济于事。我很感激任何人都可以提供帮助。提前致谢。

编辑#1

我遵循Daniel的建议来扩展测试运行器并检查VerifyError,并得到以下堆栈跟踪:

java.lang.ExceptionInInitializerError
            at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)
            at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57)
            at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)
            at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)
            at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
            at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
            at org.mockito.Mockito.mock(Mockito.java:1285)
            at org.mockito.Mockito.mock(Mockito.java:1163)
            at com.mypackage.injection.EspressoRemoteResourcesModule.<init>(EspressoRemoteResourcesModule.java:17)
            at com.mypackage.injection.MyCustomEspressoApplication.getEspressoRemoteResourcesModule(MyCustomEspressoApplication.java:52)
            at com.mypackage.injection.MyCustomEspressoApplication.getModules(MyCustomEspressoApplication.java:24)
            at com.mypackage.injection.MyCustomApplication.onCreate(MyCustomApplication.java:18)
            at com.mypackage.injection.MyCustomEspressoApplication.onCreate(MyCustomEspressoApplication.java:16)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
            at android.app.ActivityThread.access$1300(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils
            at org.mockito.cglib.core.KeyFactory$Generator.generateClass(KeyFactory.java:167)
            at org.mockito.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:25)
            at org.mockito.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:217)
            at org.mockito.cglib.core.KeyFactory$Generator.create(KeyFactory.java:145)
            at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:117)
            at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:109)
            at org.mockito.cglib.core.KeyFactory.create(KeyFactory.java:105)
            at org.mockito.cglib.proxy.Enhancer.<clinit>(Enhancer.java:70)
            at org.mockito.internal.creation.cglib.ClassImposterizer.createProxyClass(ClassImposterizer.java:95)
            at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:57)
            at org.mockito.internal.creation.cglib.ClassImposterizer.imposterise(ClassImposterizer.java:49)
            at org.mockito.internal.creation.cglib.CglibMockMaker.createMock(CglibMockMaker.java:24)
            at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:33)
            at org.mockito.internal.MockitoCore.mock(MockitoCore.java:59)
            at org.mockito.Mockito.mock(Mockito.java:1285)
            at org.mockito.Mockito.mock(Mockito.java:1163)
            at com.mypackage.injection.EspressoRemoteResourcesModule.<init>(EspressoRemoteResourcesModule.java:17)
            at com.mypackage.injection.MyCustomEspressoApplication.getEspressoRemoteResourcesModule(MyCustomEspressoApplication.java:52)
            at com.mypackage.injection.MyCustomEspressoApplication.getModules(MyCustomEspressoApplication.java:24)
            at com.mypackage.injection.MyCustomApplication.onCreate(MyCustomApplication.java:18)
            at com.mypackage.injection.MyCustomEspressoApplication.onCreate(MyCustomEspressoApplication.java:16)
            at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:999)
            at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4151)
            at android.app.ActivityThread.access$1300(ActivityThread.java:130)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1255)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4745)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
04-29 06:40:28.594    1016-1016/? W/ActivityManager﹕ Error in app com.mypackage running instrumentation ComponentInfo{com.mypackage.test/com.mypackage.EspressoTestRunner}:
04-29 06:40:28.594    1016-1016/? W/ActivityManager﹕ java.lang.VerifyError
04-29 06:40:28.594    1016-1016/? W/ActivityManager﹕ java.lang.VerifyError: org/mockito/cglib/core/ReflectUtils

这指向了Mockito。我错过了必要的mockito和dexmaker库。

我将我的依赖项更新为:

androidTestCompile 'org.mockito:mockito-core:1.10.19'
androidTestCompile 'com.google.dexmaker:dexmaker:1.2'
androidTestCompile ('com.google.dexmaker:dexmaker-mockito:1.2') {
    exclude module: 'hamcrest-core'
    exclude module: 'mockito-core'
}
androidTestCompile('com.android.support.test.espresso:espresso-core:2.0') {
     exclude group: 'javax.inject'
}

我还覆盖了MyCustomModule,它需要包含EspressoRemoteResourcesModule。一旦我做了这件事,就开始工作了。

5 个答案:

答案 0 :(得分:29)

使用自定义检测运行器,您可以覆盖newApplication并让它从清单中实例化除默认应用程序之外的其他内容。

public class MyRunner extends AndroidJUnitRunner {
  @Override
  public Application newApplication(ClassLoader cl, String className, Context context)
      throws Exception {
    return super.newApplication(cl, MyCustomEspressoApplication.class.getName(), context);
  }
}

请务必使用您的自定义亚军名称更新testInstrumentationRunner

答案 1 :(得分:17)

花了我一整天才得到完整的答案。

第1步:覆盖AndroidJUnitRunner

public class TestRunner extends AndroidJUnitRunner
{
    @Override
    public Application newApplication(ClassLoader cl, String className, Context context)
            throws InstantiationException, IllegalAccessException, ClassNotFoundException {
        return super.newApplication(cl, TestApplication.class.getName(), context);
    }
}

第2步:替换build.gradle中现有的AndroidJunitRunner

defaultConfig {
    ...
    // testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    testInstrumentationRunner 'com.xi_zz.androidtest.TestRunner'
}

第3步:添加com.android.support.test:runner到build.gradle

androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

第4步:仅在您收到此错误时

Warning:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (25.2.0) and test app (23.1.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

然后,再添加一行:

androidTestCompile 'com.android.support:support-annotations:25.2.0'
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'

最后,测试它是否有效

@RunWith(AndroidJUnit4.class)
public class MockApplicationTest
{
    @Rule
    public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class);

    @Test
    public void testApplicationName() throws Exception
    {
        assertEquals("TestApplication", mActivityRule.getActivity().getApplication().getClass().getSimpleName());
    }
}

答案 2 :(得分:3)

我没有尝试过这种广泛的案例,但您可以尝试使用自定义规则来为每个测试用例指定自定义应用程序类,而不是针对自定义运行器应用的所有测试用例。在简单的情况下,我已成功完成以下任务:

public class ApplicationTestRule<T extends Application> extends UiThreadTestRule {
    Class<T> appClazz;
    boolean wait = false;
    T app;

    public ApplicationTestRule(Class<T> applicationClazz) {
        this(applicationClazz, false);
    }

    public ApplicationTestRule(Class<T> applicationClazz, boolean wait) {
        this.appClazz = applicationClazz;
        this.wait = wait;
    }

    @Override
    public Statement apply(final Statement base, Description description) {
        return new ApplicationStatement(super.apply(base, description));
    }

    private void terminateApp() {
        if (app != null) {
            app.onTerminate();
        }
    }

    public void createApplication() throws IllegalAccessException, ClassNotFoundException, InstantiationException {
        app = (T) InstrumentationRegistry.getInstrumentation().newApplication(this.getClass().getClassLoader(), appClazz.getName(), InstrumentationRegistry.getInstrumentation().getTargetContext());
        InstrumentationRegistry.getInstrumentation().callApplicationOnCreate(app);
    }

    private class ApplicationStatement extends Statement {

        private final Statement mBase;

        public ApplicationStatement(Statement base) {
            mBase = base;
        }

        @Override
        public void evaluate() throws Throwable {
            try {
                if (!wait) {
                    createApplication();
                }
                mBase.evaluate();
            } finally {
                terminateApp();
                app = null;
            }
        }
    }
}

然后在您的测试用例中,创建规则:

@Rule
public ApplicationTestRule<TestApplication> appRule = new ApplicationTestRule<>(TestApplication.class,true);

注意第二个参数是可选的。如果为false或停止,则每次测试用例之前都会创建自定义应用程序。如果设置为true,则需要在应用程序逻辑之前调用appRule.createApplication()

答案 3 :(得分:1)

如果要测试库模块,则可以创建一个自定义应用程序类并将其注册在测试包清单中:

root / library-module / src / androidTest / AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android">
    <application android:name="path.to.TestApplication" />
</manifest>

没有规则,没有赛跑者。

答案 4 :(得分:1)

Roboelectric Configuration here 可以保存,如果只需要单独的应用程序进行测试。以下会有所帮助

@RunWith(AndroidJUnit4::class)
@Config(application = MockApp::class)
class RegisTests {
    @Test
    fun hello(){

        val acs = ActivityScenario.launch(RegistrationActivity::class.java)
        acs.moveToState(State.CREATED)
        Truth.assertWithMessage("No created").that(State.CREATED).isEqualTo(acs.state)
   

 }
}