attempting to unit test and something with RoboGuice.newDefaultRoboModule() is failing with Roboguice 2.0b3 and Robolectric

StackOverflow https://stackoverflow.com/questions/8761020

Question

I've been looking at the astroboy example code and documentation for RoboGuice 2, and I'm honestly stumped. I hope you all can help me out with things to try. The goal here is to test the module to make sure it's loading and that the IoC is working / wired up.

I have a test that is similar to their example: http://code.google.com/p/roboguice/source/browse/astroboy/src/test/java/org/roboguice/astroboy/controller/Astroboy2Test.java?name=roboguice-2.0b3&r=ba37ef680410c64f7f1fe90f5b7b482958d276b5

Mine is different in two way... My module is in a library class, which is identical by syntax:

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

I also have the roboguice.xml in the library class in the value folder

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

The test project references the the app project, which references and exports the library project.

In the test project it's like so:

@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()));
}

During the setup, it always errors with some kind of null exception. I've broken this out, and specifically with the newDefaultRoboModule method. I know that Robolectric.application is not null, and I know that new MyTestModule is not null either. Although when stepping through the debugger, I found that MyTestModule.binder is null, so I don't know if that's an issue.

the error stack trace:

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

Where else should I look? I feel lost at how all this binds via testing.

Thanks for looking, Kelly

Was it helpful?

Solution

I needed to include the javax.inject.jar from the guice 3.0 zip with my roboguice 2.0 setup.

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

OTHER TIPS

The Roboguice project has a robolectric test in the sample application. Check it out in the source. I am using the same approach and it works just fine for me.

I would definitely suggest to upgrade to 2.x of Roboguice soon. Latest with the release..

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top