Mock/Stub super constructor invocation in Java for

2019-08-15 17:30发布

问题:

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) {
        super(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:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Location.class)
public class MyLocationTest {
    @Test
    public void methodUnderTestReturnsTrue() throws Exception {
        Location locationMock = EasyMock.createMock(Location.class);
        expectNew(Location.class, Location.class).andReturn(locationMock);
        MyLocation myLocation = new MyLocation(locationMock);
        assertTrue(myLocation.methodUnderTest());
    }
}

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?

回答1:

Taken straight from the Powermocks documentation.

Testing can then be done without invoking the EvilParent constructor.

@RunWith(PowerMockRunner.class)
@PrepareForTest(ExampleWithEvilParent.class)
public class ExampleWithEvilParentTest {

        @Test
        public void testSuppressConstructorOfEvilParent() throws Exception {
                suppress(constructor(EvilParent.class));
                final String message = "myMessage";
                ExampleWithEvilParent tested = new ExampleWithEvilParent(message);
                assertEquals(message, tested.getMessage());
        }
}