Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Trying to test maps with Robolectric

View ConnectivityBroadcastReceiver.java
1 2 3 4 5 6 7
public class ConnectivityBroadcastReceiver extends BroadcastReceiver {
 
@Override
public void onReceive(Context context, Intent intent) {
System.out.println("ConnectivityBroadcastReceiver onReceive");
}
}
View ConnectivityBroadcastReceiver.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
@Implements(value = MapActivity.class, inheritImplementationMethods = true)
public class ExtendedShadowMapActivity extends ShadowMapActivity {
public void __constructor__() {
super.__constructor__();
}
@Implementation
public void onResume() {
BroadcastReceiver connectivityBroadcastReceiver = new ConnectivityBroadcastReceiver();
registerReceiver(connectivityBroadcastReceiver, new IntentFilter());
try {
realActivity.getClass().getDeclaredField("mCalled").setBoolean(realActivity, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
View ConnectivityBroadcastReceiver.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestUtility.ANDROID_MANIFEST_XML, shadows=ExtendedShadowMapActivity.class)
public class LocationsResultsMapTest {
 
private LocationsMainActivity activity;
private ActivityController<LocationsMainActivity> activityController;
@Before
public void setUp() throws Exception {
// activityController = Robolectric.buildActivity(LocationsMainActivity.class);
activity = Robolectric.buildActivity(LocationsMainActivity.class).create().get();
}
@Test
public void test() {
}
}
View ConnectivityBroadcastReceiver.java
1 2 3 4 5 6 7 8 9 10 11
public class MockMapActivity extends MapActivity {
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
}
}

Class under test changed to MockMapActivity, still same error.

do not extend ShadowMapActivity in your custom shadow , because in this case the call of
super.constructor();
in your shadow will not have any effect. In ShadowMapActivity this method just blank.
Take code from here https://github.com/robolectric/robolectric/blob/master/src/main/java/org/robolectric/shadows/ShadowMapActivity.java
rename class name and add changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.