. f java.lang.RuntimeException: Stub! in Robolectric ~ Android Developers Blog

Friday, 7 March 2014

java.lang.RuntimeException: Stub! in Robolectric


Hello Droid Friends,
                              Have you facing following error while running a test case using
Roboelectric and Junit-4.

java.lang.RuntimeException: Stub!
at android.content.Context.<init>(Context.java:4)
at android.content.ContextWrapper.<init>(ContextWrapper.java:5)
at android.view.ContextThemeWrapper.<init>(ContextThemeWrapper.java:5)
at android.app.Activity.<init>(Activity.java:6)
at com.actionbarsherlock.app.SherlockActivity.<init>(SherlockActivity.java:21)
at com.opttown.OptTown.DrawerBaseActivity.<init>(DrawerBaseActivity.java:34)
at com.opttown.Places.MyPlaces.<init>(MyPlaces.java:63)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.fest.reflect.constructor.Invoker.newInstance(Invoker.java:77)
at org.robolectric.util.ActivityController.<init>(ActivityController.java:47)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:1409)
at com.opttown.test.MyPlacesTest.setUp(MyPlacesTest.java:25)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
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.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)


Most of stub!  exceptions are because the tests are not annotated with the correct TestRunner. For example in my case I forgot to add:
@RunWith(RobolectricTestRunner.class)

After adding the above annotation the above issue will be fixed.


  1. import static org.junit.Assert.assertNotNull;  
  2. import static org.robolectric.Robolectric.shadowOf;  
  3.   
  4. import org.junit.Assert;  
  5. import org.junit.Before;  
  6. import org.junit.Test;  
  7. import org.junit.runner.RunWith;  
  8. import org.robolectric.Robolectric;  
  9. import org.robolectric.RobolectricTestRunner;  
  10. import org.robolectric.annotation.Config;  
  11. import org.robolectric.shadows.ShadowActivity;  
  12.   
  13. import android.content.Context;  
  14.   
  15. import com.android.Places.MyPlaces;  
  16.   
  17. @RunWith(RobolectricTestRunner.class)  
  18. public class MyPlacesTest {  
  19.    
  20.  private MyPlaces activity;  
  21.  Context ctx;  
  22.  ShadowActivity shadowActivity;  
  23.    
  24.  @Before  
  25.  public void setUp() throws Exception {  
  26.   activity = Robolectric.buildActivity(MyPlaces.class)  
  27.                 .create()  
  28.                 .get();  
  29.   shadowActivity = shadowOf(activity);  
  30.   ctx = activity;  
  31.  }  
  32.    
  33.  /** 
  34.    * @testdox: for activity instance test  
  35.    * given:  
  36.    * when:  app launch 
  37.    * then: checking the instance of activity 
  38.    */  
  39.   @Test  
  40.   @Config(reportSdk = 10)  
  41.   public void testActivityInstance() {  
  42.    assertNotNull(shadowActivity.getApplicationContext());  
  43.    Assert.assertEquals("Result",activity.getApplicationContext(),sh                       adowActivity.getApplicationContext());  
  44.   }  



Hope this will help you during writing the test case for your android project.



Enjoy Coding... :) 

You may also Read this:
1. Roboelectric setup with eclipse
2. R
oboelectric AndroidManifest.xml not found error
3. Roboelectric tutorial
4. Asserting Toast message using Robolectric
.

Mukesh Kumar

Hi Guys I am from Delhi working as Web/Mobile Application Developer(Android Developer), also have knowledge of Roboelctric and Mockito ,android test driven development... Blogging has been my passion and I think blogging is one of the powerful medium to share knowledge and ideas....

0 comments:

Post a Comment

 

Copyright @ 2013 Android Developers Blog.