【问题标题】:attempting to unit test and something with RoboGuice.newDefaultRoboModule() is failing with Roboguice 2.0b3 and Robolectric尝试对 RoboGuice.newDefaultRoboModule() 进行单元测试和使用 Roboguice 2.0b3 和 Robolectric 失败
【发布时间】:2012-01-06 16:27:00
【问题描述】:

我一直在查看 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);
        }
}

我在 value 文件夹的库类中也有 roboguice.xml

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

测试项目引用应用项目,应用项目引用并导出库项目。

在测试项目中是这样的:

@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 不为空,并且我知道 new MyTestModule 也不为空。虽然在单步调试时发现 MyTestModule.binder 为空,所以不知道是不是问题。

错误堆栈跟踪:

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

我还应该去哪里看?我对所有这些如何通过测试绑定感到迷茫。

感谢收看, 凯莉

【问题讨论】:

  • 所以问题在于 Provider 的注入,这是一个 guice 3.0 类。这是否意味着我需要注入提供者?我认为 roboguice 与所有这些东西有关。你们都用 RoboGuice 2 和单元测试做什么?
  • 小更新。可悲的是:我永远无法让 roboguice 2 工作。经过很多次来回后,我最终降级为使用 roboguice 1.1.2 并且没有使用 roboletric。我希望在这方面有更多的支持,但至少现在正在运行单元测试。回顾我现在必须做的事情时,我想我必须在测试时将提供程序注入应用程序,但我只是不知道。如果有人有想法,我仍然很想知道如何使用上述内容。如果有人想让我分享我为 roboguice 1.1.2 和 android 所做的事情,我也很高兴。私信我。
  • 您是否看过 roboguice 提供的示例应用程序以及它是如何设置的。它就在那里..
  • 嗨 Manfred,很高兴你能加入。我实际上去那里参考了很多资料(我也参考了这个例子)、roboguice 组和整体网络搜索,但我就是不能让它工作。正如我上面提到的,我最终恢复到 1.1.3(最新的旧版本)。我还发现 Robolectric 不符合包含多个模块的项目,这些模块混合了库和应用程序(IntelliJ 发言)。
  • Roboguice 主干中的示例应用程序包含 Robolectric 测试,包括一个模块。举个例子还不够吗?你在 roboguice 邮件列表上发帖了吗?

标签: dependency-injection android-2.2-froyo roboguice robolectric


【解决方案1】:

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

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

【讨论】:

  • 噢噢噢!谢谢你的建议。我会试试这个,让你知道。如果这就是全部,那么[这就是]完全令人敬畏@Soulreaper。
  • 最后,我能够完成并测试它。再次感谢@Moritz!你的建议做得很好。 (很抱歉我花了这么长时间才回到这个问题)
  • 这确实花了很长时间g
  • 再次感谢! *你而不是你(很着急,因为我无法编辑我的评论)
【解决方案2】:

Roboguice 项目在示例应用程序中有一个 robolectric 测试。看看in the source.我用的是同样的方法,对我来说效果很好。

我肯定会建议尽快升级到 Roboguice 的 2.x。最新发布..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-13
    • 1970-01-01
    • 2016-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多