0

I have a custom application called MainApplication. The activity I am testing references the application context. The code looks like this:

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mAppContext = (MainApplication) getApplicationContext();

    ....

}

I am using RoboGuice 2.0 and Robolectric 2.2. Now whenever I test the activity, like

@RunWith(RobolectricTestRunner.class)
public class DetailActivityTests {
    protected MainApplication application = mock(MainApplication.class, RETURNS_DEEP_STUBS);
    protected Context context = mock(RoboActivity.class, RETURNS_DEEP_STUBS);

    @Before
    public void setup() {
        when(context.getApplicationContext()).thenReturn(application);
        when(application.getApplicationContext()).thenReturn(application);
    }

    @Test
    public void testActivity() {
        Activity activity = Robolectric.buildActivity(DetailActivity.class).create().get();
        ....
    }
}

I get an the casting issue:

 android.app.Application cannot be cast to com.xxx.xxx.xxx.MainApplication
    [junit] java.lang.ClassCastException: android.app.Application cannot be cast to com.xxx.xxx.xxx.MainApplication
    [junit]     at com.xxx.xxx.xxx.activity.DetailActivity.onCreate(DetailActivity.java:52)
    [junit]     at android.app.Activity.performCreate(Activity.java:5008)
    [junit]     at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
    [junit]     at org.robolectric.util.ActivityController$1.run(ActivityController.java:116)
    [junit]     at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:256)
    [junit]     at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    [junit]     at org.robolectric.util.ActivityController.create(ActivityController.java:123)
    [junit]     at com.xxx.xxx.xxx.activity.GlanceActivityTests.testActivity(DetailActivityTests.java:33)
    [junit]     at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
    [junit]     at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)

How can i fix this?

MDMalik
  • 3,951
  • 2
  • 25
  • 39
Grace Huang
  • 5,355
  • 5
  • 30
  • 52
  • I'm setting the application used in test to my main application using `setApplication()` – Ahmed Zayed Mar 03 '14 at 18:08
  • @AhmedHafez Could you be more specific about `setApplication()`? Thanks! – Grace Huang Mar 03 '14 at 18:20
  • if (getApplication() == null || getApplication() instanceof MockApplication) { MyApplication myApp = new MyApplication(); setApplication(myApp); myApp.loadForTesting(getSystemContext()); } – Ahmed Zayed Mar 03 '14 at 18:23
  • @AhmedHafez when should I put this code? – Grace Huang Mar 03 '14 at 18:33
  • In your service setup, this will run this test under your application – Ahmed Zayed Mar 03 '14 at 18:39
  • Hmmm, I'm still not sure I understand it. If you could give a more specific instruction, I would appreciate it a lot. – Grace Huang Mar 03 '14 at 19:10
  • As i understand, your problem is concerning to disability to cast the application class that is running the test case to your custom application instance. So, i solved this bug by setting the testing application to my application instance. You can set the running test application to your custom application using the above code. – Ahmed Zayed Mar 03 '14 at 19:26
  • 1
    I searched around and I dont think setApplication is part of Robolectric 2.0 now. – Grace Huang Mar 03 '14 at 19:45
  • Sorry for your corruption. but i was not using Robolectric and yes no set application in Robolectric. – Ahmed Zayed Mar 03 '14 at 19:52
  • @GraceShao Did you find a solution? – alfoks Jan 20 '17 at 12:24

0 Answers0