依赖注入 – 使用Roboguice 2.0b3和Robolectric尝试使用RoboGuice.newDefaultRoboModule()进行单元测试和某些操作失败

前端之家收集整理的这篇文章主要介绍了依赖注入 – 使用Roboguice 2.0b3和Robolectric尝试使用RoboGuice.newDefaultRoboModule()进行单元测试和某些操作失败前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在看RoboGuice 2的astroboy示例代码和文档,老实说我很难过.我希望你们都可以帮我解决一些问题.这里的目标是测试模块以确保它正在加载并且IoC正在工作/接线.

我有一个类似于他们的例子的测试:
http://code.google.com/p/roboguice/source/browse/astroboy/src/test/java/org/roboguice/astroboy/controller/Astroboy2Test.java?name=roboguice-2.0b3&r=ba37ef680410c64f7f1fe90f5b7b482958d276b5

我的两种方式不同……我的模块在一个库类中,它的语法相同:

public class MyTestModule extends AbstractModule {
        @Override
        protected void configure() {
            bind(Vibrator.class).toInstance(vibratorMock);
        }
}

我还在值文件夹中的库类中有roboguice.xml

<resources> 
    <string-array name=roboguice_modules> 
        <item>com.yourdomain.MyTestModule</item> 
    </string-array> 
<resources>

测试项目引用app项目,该项目引用并导出库项目.

在测试项目中它是这样的:

@RunWith(RobolectricTestRunner.class)
public class MyTest {

    @Before
    public void setup() {
    // Override the default RoboGuice module
    RoboGuice.setBaseApplicationInjector(Robolectric.application,RoboGuice.DEFAULT_STAGE,Modules.override(RoboGuice.newDefaultRoboModule(Robolectric.application)).with(new MyTestModule()));
}

在设置过程中,它总是出现错误,带有某种空异常.我已经解决了这个问题,特别是newDefaultRoboModule方法.我知道Robolectric.application不是null,我知道新的MyTestModule也不是null.虽然在单步执行调试器时,我发现MyTestModule.binder为null,所以我不知道这是不是问题.

错误堆栈跟踪:

java.lang.NoClassDefFoundError: javax/inject/Provider
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at javassist.Loader.findClass(Loader.java:379)
at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.findClass(RobolectricClassLoader.java:72)
at javassist.Loader.loadClass(Loader.java:311)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:49)
at roboguice.RoboGuice.newDefaultRoboModule(RoboGuice.java:144)
at test.yourdomain.MyTest.setup(MyTest.java:45)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
at com.xtremelabs.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:284)
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.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: javax.inject.Provider
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at javassist.Loader.delegateToParent(Loader.java:428)
at javassist.Loader.loadClassByDelegation(Loader.java:406)
at javassist.Loader.loadClass(Loader.java:308)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.xtremelabs.robolectric.bytecode.RobolectricClassLoader.loadClass(RobolectricClassLoader.java:49)
... 36 more

我还应该在哪儿看?我对通过测试所有这些结合感到迷茫.

谢谢你的期待,
黄绿色

我需要在我的roboguice 2.0设置中包含来自guice 3.0 zip的javax.inject.jar.

https://github.com/google/guice/wiki/Guice30

猜你在找的设计模式相关文章