Getting error in Application class during unit testing

I am getting weird exception when i am trying to execute my application class through robolectric. I am stuck from several hours on it. Could someone please help me on it? My code snippet is given below:

 java.lang.RuntimeException: Stub!

at android.test.ApplicationTestCase.__constructor__(ApplicationTestCase.java:5)
at android.test.ApplicationTestCase.<init>(ApplicationTestCase.java)
at com.grapplemobile.tmcplus.TmcPlusApplicationTest.<init>(TmcPlusApplicationTest.java:26)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.createTest(RobolectricTestRunner.java:520)
at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
at org.robolectric.RobolectricTestRunner$HelperTestRunner.methodBlock(RobolectricTestRunner.java:530)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:247)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:152)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

My Code is following:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,sdk = 18)
public class MyApplicationTest extends ApplicationTestCase<MyApplication>{
  private MyApplication application;

   public  MyApplicationTest ()
  {
      super(MyApplication.class);
   }
   @Before
    public void setUp() throws Exception {
      // super.setUp();
      createApplication();
      application = getApplication();

   }

   @Test
   public void testonCreate() throws Exception {
   application.onCreate();
  }

Answer

Don’t mix JUnit tests with instrumental tests:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class,sdk = 18)
public class MyApplicationTest{
}

If you need the instance of the application then use RuntimeEnvironment.application. If you want to test Application itself then just create it with new and call methods on it, but it should be not Robolectric test

Leave a Reply

Your email address will not be published. Required fields are marked *