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?