java.lang.InstantiationException - Roboelectric Beginner...

637 views
Skip to first unread message

Joe P.

unread,
Nov 26, 2013, 9:15:35 AM11/26/13
to robol...@googlegroups.com
Hello,

I am just getting started with Roboelectric.  I have my test project set up "AndroidProjectTest".  I am not using maven and have added the following jar files:

android.jar
junit-4.11.jar
hamcrest-all-1.3.jar
robolectric-2.2-jar-with-dependencies.jar

I created a very simple unit test that does nothing but creates a MockContext:

@Test
public void sampleTest() {
    MockContext context = new MockContext();
}

When I run the test case, I get the following exception:

java.lang.RuntimeException: java.lang.InstantiationException
at org.robolectric.bytecode.ShadowWrangler.createShadowFor(ShadowWrangler.java:355)
at org.robolectric.bytecode.ShadowWrangler.initializing(ShadowWrangler.java:78)
at org.robolectric.bytecode.RobolectricInternals.initializing(RobolectricInternals.java:72)
at android.test.mock.MockContext.$$robo$init(MockContext.java)
at android.content.Context.<init>(Context.java:51)
at android.test.mock.MockContext.<init>(MockContext.java:5)
at com.myapp.SampleTest.sampleTest(SampleTest.java:32)
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:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:234)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:175)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
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.InstantiationException
at sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(InstantiationExceptionConstructorAccessorImpl.java:48)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at java.lang.Class.newInstance0(Class.java:374)
at java.lang.Class.newInstance(Class.java:327)
at org.robolectric.bytecode.ShadowWrangler.createShadowFor(ShadowWrangler.java:348)
... 31 more


Am I missing some dependency?  How can I resolve this issue?

Thanks,

Joe

Erich Douglass

unread,
Nov 26, 2013, 10:52:19 AM11/26/13
to robol...@googlegroups.com
Hi Joe,

In your test, you are creating a MockContext which is a class used with Google's InstrumentationTest framework.  With Robolectric, you can instantiate a context directly (e.g. new Context) or use the Application context (e.g. Robolectric.application).  I have a feeling the MockContext class isn't playing nicely with Robolectric's classloader.

If you want to see an example of how to write tests for Robolectric, check out http://github.com/robolectric/RobolectricSample.  The project is a bit out of date, but it should give you an idea of how to write tests.


--
You received this message because you are subscribed to the Google Groups "Robolectric" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robolectric...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Joe P.

unread,
Nov 26, 2013, 12:06:08 PM11/26/13
to robol...@googlegroups.com
That worked thanks.  One more quick question... is there any way to pipe the logging output to standard out?

-Joe

Erich Douglass

unread,
Nov 26, 2013, 4:20:50 PM11/26/13
to robol...@googlegroups.com
Do you mean the test runner's output or stuff like Log.d, Log.i, etc?
Reply all
Reply to author
Forward
0 new messages