Take the 2-minute tour ×

I want to unit-test my class with JUnit and EasyMock. It extends android.location.Location. But I am always getting Stub! exception because most of Android methods are not available in JVM runtime.

public class MyLocation extends Location {
    public MyLocation(Location l) {

    public boolean methodUnderTest() {
        return true;

I've tried to mock constructor invocation using Powermock, but it looks like it does not work for super calls. My test:

public class MyLocationTest {
    public void methodUnderTestReturnsTrue() throws Exception {
        Location locationMock = EasyMock.createMock(Location.class);
        expectNew(Location.class, Location.class).andReturn(locationMock);
        MyLocation myLocation = new MyLocation(locationMock);

An exception I am getting:

java.lang.RuntimeException: Stub!
    at android.location.Location.<init>(Location.java:6)

Obviously the solution is to execute this test in Android runtime (i.e. start Android Simulator). But I don't like this approach because it takes quite a few time to start such test suite. Is there a way to stub super invocation or probably there's better approach in testing such implementations?

share|improve this question
Mock a superclass constructor –  atomman Oct 8 '13 at 20:47

1 Answer 1

up vote 1 down vote accepted

Taken straight from the Powermocks documentation.

Testing can then be done without invoking the EvilParent constructor.

public class ExampleWithEvilParentTest {

        public void testSuppressConstructorOfEvilParent() throws Exception {
                final String message = "myMessage";
                ExampleWithEvilParent tested = new ExampleWithEvilParent(message);
                assertEquals(message, tested.getMessage());
share|improve this answer
This works perfectly, thank you. I somehow overlooked this method (tried it previously, but considered non-working as well). –  Andrey Chernih Oct 9 '13 at 4:13

Your Answer


