我一直在查看 RoboGuice 2 的 astroboy 示例代码和文档,我真的很难过。我希望你们都可以帮助我尝试一些事情。这里的目标是测试模块以确保它正在加载并且 IoC 正在工作/连接。
我的有两种不同...我的模块在一个库类中,语法相同:
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>
测试项目引用了应用程序项目,该项目引用并导出了库项目。
在测试项目中是这样的:
@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
我还应该去哪里看?我对所有这些如何通过测试绑定感到迷茫。
感谢您的关注,凯利