【发布时间】:2012-01-06 16:27:00
【问题描述】:
我一直在查看 RoboGuice 2 的 astroboy 示例代码和文档,老实说,我很难过。我希望你们都可以帮助我尝试一些事情。这里的目标是测试模块以确保它正在加载并且 IoC 正在工作/连接。
我的在两个方面有所不同...我的模块在一个库类中,语法相同:
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