5

When trying to run a JUnit test on an Android Library project with Robolectric i get the following error:

java.lang.RuntimeException: java.lang.InstantiationException
    at com.xtremelabs.robolectric.ApplicationResolver.newApplicationInstance(ApplicationResolver.java:56)
    at com.xtremelabs.robolectric.ApplicationResolver.resolveApplication(ApplicationResolver.java:36)
    at com.xtremelabs.robolectric.RobolectricTestRunner.createApplication(RobolectricTestRunner.java:246)
    at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:233)
    at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:177)
    at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:157)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    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(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at com.xtremelabs.robolectric.ApplicationResolver.newApplicationInstance(ApplicationResolver.java:54)
    ... 20 more

My first idea was that i made a mistake within the test, so i simplified the test run as much as possible:

import static org.junit.Assert.*;

import org.junit.Test;
import org.junit.runner.RunWith;

import com.xtremelabs.robolectric.RobolectricTestRunner;

@RunWith(RobolectricTestRunner.class)
public class CommonDataTest {

    @Test
    public void test(){
        assertTrue(true);
    }

}

but i still get the same error. I set up the Project as described here: http://robolectric.org/eclipse-quick-start/ The strange thing is that it worked with other Android Library Projects without any problems.

Any help would be appreciated! Thank you in advance!

2
  • I followed that guide (in exception for changing the src/ folder) without any problems. try to put Robolectric first in the build path order Commented Aug 22, 2012 at 15:11
  • robolectric.org/getting_started states today (2014-03-09) that the eclipse quick start guide is LIKELY OUT OF DATE. "If you try this or get it working otherwise, please let us know how and help us make the Deckards work for Eclipse". Commented Mar 9, 2014 at 10:07

1 Answer 1

4

it seemed that Robolectric was unable to create the Application from my library so i replaced the TestRunner:

import org.junit.runners.model.InitializationError;

import android.app.Application;

import com.xtremelabs.robolectric.RobolectricTestRunner;

public class MyTestRunner extends RobolectricTestRunner {

    public MyTestRunner(Class<?> testClass)
            throws InitializationError {
        super(testClass);
    }

    @Override protected Application createApplication() {
        return new Application() {
        };
    }


}

and used it instead of the RobolectricTestRunner:

@RunWith(MyTestRunner.class)

The tests are running properly now.

Sign up to request clarification or add additional context in comments.

1 Comment

I was also having a problem with Robolectric and Application. Instead of introducing MyTestRunner, what about sticking with @RunWith(RobolectricTestRunner.class) and using Robolectric.application = new Application();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.