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

android – 我可以在Espresso中扩展自定义应用程序吗?,第1张

概述我正在尝试在我的Espresso仪器测试中设置匕首,以嘲笑外部资源的调用(在这种情况下为RESTful服务).在Robolectric进行单元测试时,我使用的模式是扩展我的生产应用程序类,并用测试模块覆盖Dagger模块,该模块将返回嘲讽.我试图在这里做同样的事情,但是当我尝试将应用程序转换到我的自定义应用程序时,我的Espresso测试中会收到ClassCastException. 这是我到目前 我正在尝试在我的Espresso仪器测试中设置匕首,以嘲笑外部资源的调用(在这种情况下为RESTful服务).在Robolectric进行单元测试时,我使用的模式是扩展我的生产应用程序类,并用测试模块覆盖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 bevitypublic class MyActivity extends MyBaseActivity {    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //snip    }}

浓咖啡设置

在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)@LargeTestpublic 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的许多答案,其中大部分都是在Application节点上指出一个缺少的“androID:name”属性,我把这个粘贴在这里表明不是这样的据我所知.)

<?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')}

堆栈跟踪:

java.lang.classCastException:
com.mypackage.injection.MyCustomApplication cannot be cast to
com.mypackage.injection.MyCustomEspressoApplication at
com.mypackage.espresso.MyActivityTest.testWhenTheActionbarbuttonIspressedThenThePlacesAreListed(MyActivityTest.java:107)
at 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)
at
org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at
org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at
org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at
org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at
org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263) at
org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:68)
at
org.junit.runners.BlockJUnit4ClassRunner.runchild(BlockJUnit4ClassRunner.java:47)
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.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: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.access00(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.access00(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.VerifyError04-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.一旦我这样做,事情就开始工作了.

解决方法 使用自定义检测运行器,您可以覆盖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.

总结

以上是内存溢出为你收集整理的android – 我可以在Espresso中扩展自定义应用程序吗?全部内容,希望文章能够帮你解决android – 我可以在Espresso中扩展自定义应用程序吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1131477.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存