我试图在我的Espresso检测测试中设置Dagger,以便模拟对外部资源(在本例中是RESTful服务)的调用。我在Robolectric中为我的单元测试所遵循的模式是扩展我的生产应用程序类,并用返回mock的测试模块重写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);
}
}
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
}
}
在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;
}
}
我的浓缩咖啡测试,在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
}
}
<?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.MyCustomesPressoApplication不能在com.mypackage.espresso.MyActivityTest.TestWhentheActionBarbuttonisPressedThePlaceSareList(myActivityTest.java:107)在java.lang.reflect.Method.Invoke(Native Method)(myActivityTest.java:107)在java.lang.reflect.Method.jave:511)在Allable.java:15)在org.junit.runners.model.frameworkmethod.InvokeExployl(frameworkmethod.java:42)在org.junit.internal.runners.statements.invokeMethod.evaluate(invokeMethod.java:20)在org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:28)在blockJunit4ClassRunner.java:68)在org.junit.runners.blockJunit4ClassRunner.runchild(blockJunit4ClassRunner.java:47)在org.junit.runners.parentRunner$3.run(parentRunner.java:231)在org.junit.runners.parentRunner$1在org.junit.runner.parentRunner$3.run(parentRunner.java:231)在parentrunner.java:300)在org.junit.runners.suite.runchild(Suite.java:128)在org.junit.runners.suite.runchild(Suite.java:24)在org.junit.runners.parentrunner$3.run(parentrunner.java:231)在org.junit.runners.parentrunner$1在org.junit.runners.parentrunner$3.run(parentrunner.java:60)在runners.parentrunner.run(parentrunner。java:300)在org.junit.runner.junitcore.run(junitcore.java:157)在org.junit.runner.junitcore.run(junitcore.java:136)在android.support.test.runner.androidJunitrunner.onstart(androidJunitrunner.java:270)在android.app.instrumentation$instrumentationthread.run(instrumentation.java:1551)
我通读了浓缩咖啡和匕首的文档,并在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
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'
}
使用自定义检测运行程序,您可以重写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
。
当我试图编写一个与listview项交互的代码时,我得到了一个OmbyuousViewMatcherException异常。场景如下。 我有一个包含两个视图的listview 文本视图 按钮查看 我在列表中有将近250行。所有按钮都有文本“预订”或“取消”。他们在一个洗牌的顺序。我想直接点击列表中的第一个“预订它”按钮。我已经尝试了很多方案,但仍然无法找到解决方案。拜托谁帮帮我。 下面是我现在的代
在最近迁移到Dagger2之后,我正在开发的应用程序的每个功能都使用了@ActivityScope。每个应用程序特性都是使用MVP模式实现的,并且有自己的本地dagger组件设置,它依赖于应用程序组件,以获得整个应用程序生命周期(由应用程序提供)所需的依赖关系。每个特性的activity都将一个提供主要应用程序组件的基类扩展到一个由每个activity覆盖的方法,以便设置本地dagger组件(构建
在我的主要活动中,我有initUi函数,它将触发对webviewActivity的意图,在webviewActivity中,有一个FragWebView,其中加载了url。 以下是来自FragWebView的示例代码: 我从我的主要活动中传递打开webview的意图是: 请让我知道如何解决这个问题。 问候
null 动画/转换被禁用,但这不会阻止CircularProgressLayout动画化。 我尝试,但即使这样,在第一次单击和第二次单击之间也要等待2秒。 我还试图滥用作为黑客来确认退出应用程序。这确实有效,但这是一个黑客。我只想让浓缩咖啡点击两次。 有什么建议如何使浓缩咖啡点击两次而不等待动画?
我试着用“浓缩咖啡”写简单的测试 但我有个错误: 我正在尝试不同的框架进行测试,对我来说是最好的,但如果有人能帮助修复这个错误,我将非常感激
我正在使用espresso进行测试,在一个页面中有多个AdapterViews,例如,ID:r.id.list1,r.id.list2 如何选择特定的listview并在其上尝试onData?