score:0

unfortunately this is a bug in android itself. see the bug report here. the vm crashes when it tries to look for annotations on a proxy, which is what androidmock uses when mocking an interface.

the workaround is to create in instance of a class that implements the interface, as you pointed out in your question. you could try making a pure abstract class that implements the interface without implementing any methods, then use androidmock to mock that class instead of the interface. this should avoid the creation of a proxy.

score:5

unfortunately, if there's an issue with the setup steps for roboguice & unit testing, you can get this sort of error. no magic short answer, but rather a set of steps to follow exactly.

btw, you're using roboguice 1.1 - abstractandroidmodule & robounittest no longer exist in roboguice 2.0. roboguice 1.1 is deprecated, so best overall solution is to move to newest version according to these instructions upgrading to 2.0.

however, just in case you're attached to roboguice 1.1, here's some steps to follow:

  • don't have inconsistent generated code/build files after refactoring/changing package names etc. delete generated code, do a clean & build, even recreate a new project and copy source files in.
  • have your app code in one project (roboguice dependent, instrumentation/robounittestcase/androidmock independent). you app code project has within lib: guice-2.0-no_aop.jar and roboguice-1.1.2.jar.
  • have your unit test code in another project that references it (roboguice independent, instrumentation/robounittestcase/androidmock independent). instructions here before you get started. your test code project has within lib: androidmockgenerator.jar.
  • in your app project, your app + module classes look something like this:

    package com.mypackage;
    
    import android.app.instrumentation;
    import android.content.context;
    
    public class myapplication extends roboguice.application.roboapplication {
    
    static mymodule mymodule;    
    
    // this constructor usually called by app
    public myapplication(context context) {
        super();
        attachbasecontext(context);
    }
    // this constructor called by unit tests.  this is unfortunately small amount of 
    // 'abstraction leakage' of unit test needs into app code.
    public myapplication(instrumentation instrumentation) {
        super();
        attachbasecontext(instrumentation.getcontext());
    }    
    public static void setmodule(mymodule module) {
        myapplication.mymodule = module;
    }   
    public static mymodule getmodule() {
        return myapplication.mymodule;
    }   
    }
    

    and

    package com.mypackage;
    
    public class mymodule extends roboguice.config.abstractandroidmodule {
    // this will be injected
    protected usefulobject myusefulinstance;    
    
    public void setusefulobject(usefulobject usefulinstance) {
        this.myusefulinstance = usefulinstance;
    }    
    public usefulobject getusefulobject() {
        return this.myusefulinstance;
    }    
    
    @override
    protected void configure() {
        bind(usefulobject.class).toinstance(myusefulinstance);
    }
    

    }

  • in your test project, your test case class looks something like this:

    import android.test.suitebuilder.annotation.largetest;    
    import com.mypackage.myapplication;    
    import com.mypackage.mymodule;    
    import com.mypackage.usefulobject;    
     //import com.mypackage.usefulobjectsimpleimplementation;    
    import android.test.suitebuilder.annotation.mediumtest;    
    import android.test.suitebuilder.annotation.smalltest;    
    import com.google.android.testing.mocking.androidmock;    
    import roboguice.test.robounittestcase;
    
    public class testmymodule extends robounittestcase<myapplication> {
    
    @override
    protected void setup() throws exception {
        usefulobject instance = // new usefulobjectsimpleimplementation(); 
                                androidmock.createnicemock(usefulobject.class);           
        mymodule mockmodule = new mymodule();
        mockmodule.setusefulobject(instance);
        myapplication.setmodule(mockmodule);
        super.setup();
    }
    
    // make sure you use one of the @*test annotations and begin
    // your testcase's name with "test"
    @mediumtest
    public void test01() {
        androidmock.expect(myapplication.getmodule().getusefulobject().
             simplemethod()).andstubreturn("hello!");
    }
    

    }

  • ensure that for the test project, the androidmanifest.xml file has the following entry:

   <instrumentation android:name="android.test.instrumentationtestrunner"
     android:targetpackage="com.mypackage"
     android:label="tests for com.mypackage"/>
  • before running your test, ensure your emulator is started and is running healthily, by running a different, simple "hello world" app first. when this succeeds, then run your app. lastly, run your test project.

should work after this. best of luck & let me know!


Related Query

More Query from same tag