java – 在创建RoboGuice注入器期间,如果在任何模块中有一个模拟实例,进程崩溃

我有一个问题,在单元测试中使用RoboGuice和AndroidMock框架。
我创建了一个简单的项目来显示我的问题。这里我创建一个模拟实例并在RoboGuice中注册它。
但是,“setUp()”和“test01()”方法之间的进程崩溃。
我猜,实际上,当创建Injector时,进程崩溃,如果任何模块里面有一个模拟的实例。

如果我用实现接口的类的实例替换mocked实例,那么一切都正常。

有人知道如何解决这个问题吗?

这里是我的测试代码:

public class testInjectMock extends RoboUnitTestCase<MyApplication> {
    protected void setUp() throws Exception {
        InterfaceToMock instance = AndroidMock.createNiceMock(InterfaceToMock.class);           AndroidMock.expect(instance.SimpleMethod()).andStubReturn("Hello!");            
        MyModule myMockModule = new MyModule();
        myMockModule.setMockedInstance(instance);//Comment this string to get into the test01() method          
        MyApplication.setMyModule(myMockModule);
        super.setUp();
    }
    public void test01() {
        //It never comes here
    }
}

模块源代码:

public class MyModule extends AbstractAndroidModule {
        protected InterfaceToMock mockedInstance;
        public void setMockedInstance(InterfaceToMock mockedInstance) {
            this.mockedInstance = mockedInstance;
        }
        @Override
        protected void configure() {
            if(mockedInstance != null)
                bind(InterfaceToMock.class).toInstance(mockedInstance);
        }
    }

logcat输出:

05-23 16:17:07.135: INFO/DEBUG(27): Build fingerprint: 'generic/sdk/generic/:2.1-update1/ECLAIR/35983:eng/test-keys'
05-23 16:17:07.135: INFO/DEBUG(27): pid: 2025, tid: 2031  >>> InjectMock.test <<<
05-23 16:17:07.145: INFO/DEBUG(27): signal 11 (SIGSEGV), fault addr 00000000
05-23 16:17:07.155: INFO/DEBUG(27):  r0 0011b218  r1 43d1caa0  r2 00000000  r3 00000000
05-23 16:17:07.155: INFO/DEBUG(27):  r4 43d1caa0  r5 0011b218  r6 451c0e30  r7 4000a958
05-23 16:17:07.155: INFO/DEBUG(27):  r8 ad00f380  r9 00138de0  10 426bda34  fp 00138de0
05-23 16:17:07.155: INFO/DEBUG(27):  ip 00000002  sp 451c0dc0  lr ad05ad1d  pc ad05a804  cpsr 00000030
05-23 16:17:07.295: INFO/DEBUG(27):          #00  pc 0005a804  /system/lib/libdvm.so
05-23 16:17:07.305: INFO/DEBUG(27):          #01  pc 0005ad18  /system/lib/libdvm.so
05-23 16:17:07.305: INFO/DEBUG(27):          #02  pc 00054a4a  /system/lib/libdvm.so
05-23 16:17:07.315: INFO/DEBUG(27):          #03  pc 00013f58  /system/lib/libdvm.so
05-23 16:17:07.325: INFO/DEBUG(27):          #04  pc 00019888  /system/lib/libdvm.so
05-23 16:17:07.335: INFO/DEBUG(27):          #05  pc 00018d5c  /system/lib/libdvm.so
05-23 16:17:07.335: INFO/DEBUG(27):          #06  pc 0004d6d0  /system/lib/libdvm.so
05-23 16:17:07.345: INFO/DEBUG(27):          #07  pc 0004d702  /system/lib/libdvm.so
05-23 16:17:07.355: INFO/DEBUG(27):          #08  pc 00041c78  /system/lib/libdvm.so
05-23 16:17:07.365: INFO/DEBUG(27):          #09  pc 00010000  /system/lib/libc.so
05-23 16:17:07.365: INFO/DEBUG(27):          #10  pc 0000fad4  /system/lib/libc.so
05-23 16:17:07.375: INFO/DEBUG(27): code around pc:
05-23 16:17:07.385: INFO/DEBUG(27): ad05a7f4 ffff5ae0 fffe57c4 6801b5f8 6a8b1c05 
05-23 16:17:07.385: INFO/DEBUG(27): ad05a804 1c30681e ff5ef7ff 28001c04 6840d018 
05-23 16:17:07.395: INFO/DEBUG(27): ad05a814 d0152800 37101c27 d0112f00 f7ff1c28 
05-23 16:17:07.395: INFO/DEBUG(27): code around lr:
05-23 16:17:07.405: INFO/DEBUG(27): ad05ad0c f7ff1c20 bd10ff7b 6804b510 fd70f7ff 
05-23 16:17:07.405: INFO/DEBUG(27): ad05ad1c 28001c01 f7ffd102 e002f859 f7ff1c20 
05-23 16:17:07.415: INFO/DEBUG(27): ad05ad2c bd10ff6d 4c24b5f0 1c0d1c06 48236a81 
05-23 16:17:07.425: INFO/DEBUG(27): stack:
05-23 16:17:07.425: INFO/DEBUG(27):     451c0d80  43d20870  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
05-23 16:17:07.425: INFO/DEBUG(27):     451c0d84  00000354  
05-23 16:17:07.425: INFO/DEBUG(27):     451c0d88  00000022  
05-23 16:17:07.425: INFO/DEBUG(27):     451c0d8c  ad043693  /system/lib/libdvm.so
05-23 16:17:07.425: INFO/DEBUG(27):     451c0d90  ad07ff50  /system/lib/libdvm.so
05-23 16:17:07.425: INFO/DEBUG(27):     451c0d94  00000024  
05-23 16:17:07.425: INFO/DEBUG(27):     451c0d98  00000354  
05-23 16:17:07.425: INFO/DEBUG(27):     451c0d9c  ad0170ac  /system/lib/libdvm.so
05-23 16:17:07.425: INFO/DEBUG(27):     451c0da0  00000000  
05-23 16:17:07.435: INFO/DEBUG(27):     451c0da4  afe0f2c0  /system/lib/libc.so
05-23 16:17:07.435: INFO/DEBUG(27):     451c0da8  ad080c00  /system/lib/libdvm.so
05-23 16:17:07.435: INFO/DEBUG(27):     451c0dac  00000002  
05-23 16:17:07.435: INFO/DEBUG(27):     451c0db0  00000354  
05-23 16:17:07.445: INFO/DEBUG(27):     451c0db4  43d20870  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
05-23 16:17:07.445: INFO/DEBUG(27):     451c0db8  df002777  
05-23 16:17:07.455: INFO/DEBUG(27):     451c0dbc  e3a070ad  
05-23 16:17:07.455: INFO/DEBUG(27): #00 451c0dc0  00000000  
05-23 16:17:07.455: INFO/DEBUG(27):     451c0dc4  43d1caa0  /dev/ashmem/mspace/dalvik-heap/2 (deleted)
05-23 16:17:07.455: INFO/DEBUG(27):     451c0dc8  451c0e38  
05-23 16:17:07.455: INFO/DEBUG(27):     451c0dcc  451c0e30  
05-23 16:17:07.455: INFO/DEBUG(27):     451c0dd0  4000a958  /dev/ashmem/mspace/dalvik-heap/zygote/0 (deleted)
05-23 16:17:07.455: INFO/DEBUG(27):     451c0dd4  ad05ad1d  /system/lib/libdvm.so
05-23 16:17:07.465: INFO/DEBUG(27): #01 451c0dd8  417a0b5c  /data/dalvik-cache/system@framework@core.jar@classes.dex
05-23 16:17:07.475: INFO/DEBUG(27):     451c0ddc  ad054a4f  /system/lib/libdvm.so
不幸的是,如果RoboGuice和安装程序的设置步骤有问题,单元测试,可以得到这种错误。没有魔术的简短的答案,而是一套完全遵循的步骤。

BTW,你使用RoboGuice 1.1 – AbstractAndroidModule& RoboUnitTest在RoboGuice 2.0中不再存在。 RoboGuice 1.1已弃用,因此最佳的整体解决方案是根据这些说明Upgrading to 2.0移动到最新版本。

但是,为了防止您附加到RoboGuice 1.1,请按照以下步骤操作:

>重构/更改软件包名称后生成的代码/构建文件不一致等。删除生成的代码,清理&构建,甚至重新创建一个新项目并复制源文件。
>在一个项目中有你的应用程序代码(RoboGuice依赖,Instrumentation / RoboUnitTestCase / AndroidMock独立)。你的应用程序代码项目在lib:guice-2.0-no_aop.jar和roboguice-1.1.2.jar中。
>让你的单元测试代码在另一个引用它的项目中(RoboGuice independent,Instrumentation / RoboUnitTestCase / AndroidMock independent)。这里的说明Before You Get Started.您的测试代码项目在lib:AndroidMockGenerator.jar。
>在您的应用程序项目中,您的应用程序模块类看起来像这样:

package com.mypackage;

import android.app.Instrumentation;
import android.content.Context;

public class MyApplication extends roboguice.application.RoboApplication {

static MyModule myModule;    

// this constructor usually called by app
public MyApplication(Context context) {
    super();
    attachBaseContext(context);
}
// This constructor called by unit tests.  This is unfortunately small amount of 
// 'abstraction leakage' of unit test needs into app code.
public MyApplication(Instrumentation instrumentation) {
    super();
    attachBaseContext(instrumentation.getContext());
}    
public static void setModule(MyModule module) {
    MyApplication.myModule = module;
}   
public static MyModule getModule() {
    return MyApplication.myModule;
}   
}

package com.mypackage;

public class MyModule extends roboguice.config.AbstractAndroidModule {
// this will be injected
protected UsefulObject myUsefulInstance;    

public void setUsefulObject(UsefulObject usefulInstance) {
    this.myUsefulInstance = usefulInstance;
}    
public UsefulObject getUsefulObject() {
    return this.myUsefulInstance;
}    

@Override
protected void configure() {
    bind(UsefulObject.class).toInstance(myUsefulInstance);
}

}}
>在你的测试项目中,你的测试用例类看起来像这样:

import android.test.suitebuilder.annotation.LargeTest;    
import com.mypackage.MyApplication;    
import com.mypackage.MyModule;    
import com.mypackage.UsefulObject;    
 //import com.mypackage.UsefulObjectSimpleImplementation;    
import android.test.suitebuilder.annotation.MediumTest;    
import android.test.suitebuilder.annotation.SmallTest;    
import com.google.android.testing.mocking.AndroidMock;    
import roboguice.test.RoboUnitTestCase;

public class TestMyModule extends RoboUnitTestCase<MyApplication> {

@Override
protected void setUp() throws Exception {
    UsefulObject instance = // new UsefulObjectSimpleImplementation(); 
                            AndroidMock.createNiceMock(UsefulObject.class);           
    MyModule mockModule = new MyModule();
    mockModule.setUsefulObject(instance);
    MyApplication.setModule(mockModule);
    super.setUp();
}

// Make sure you use one of the @*Test annotations AND begin
// your testcase's name with "test"
@MediumTest
public void test01() {
    AndroidMock.expect(MyApplication.getModule().getUsefulObject().
         simpleMethod()).andStubReturn("Hello!");
}

}}
>确保对于测试项目,AndroidManifest.xml文件具有以下条目:

06003

>在运行测试之前,首先运行一个不同的,简单的“Hello World”应用程序,确保您的模拟器启动并运行健康。成功后,运行您的应用程序。最后,运行测试项目。

应该在此之后工作。最好的运气让我知道!

http://stackoverflow.com/questions/6101310/process-crashes-during-creation-of-roboguice-injector-if-there-is-a-mocked-inst

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:java – 在创建RoboGuice注入器期间,如果在任何模块中有一个模拟实例,进程崩溃