作者:原文W | 来源:互联网 | 2023-06-28 13:42
It seems that any test that attempts to call
for a supported object type (e.g.,
) fails to load the
class because of some classes that are `` in the Android platform. The errors are similar to:
1 2 3 4
| /path/to/MyTest.java:184: error: cannot access ActivityThread
Assert.assertTrue(shadowOf((View) mView).didRequestLayout());
^
class file for android.app.ActivityThread not found |
The cast of
was added only to show that it's actually a View object, and should be calling
. I'm not accessing
at all, but looking at the class in the IDE, it is documented as
1
| `, and it cannot be accessed from within my application code. Opening the Shadows class in the IDE also shows several errors related to the classes that cannot be loaded, including |
ActivityThread`.
The same error occurs for other calls to
, each with a different problem class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| /path/to/MyTest.java:184: error: cannot access ActivityThread
Assert.assertTrue(shadowOf((View) mView).didRequestLayout());
^
class file for android.app.ActivityThread not found
/path/to/MyTest.java:193: error: cannot access AlertController
ShadowAdapterView shadow = shadowOf(mView);
^
class file for com.android.internal.app.AlertController not found
/path/to/MyTest.java:211: error: cannot access DisplayManagerGlobal
shadowOf(mView).performItemClick(2);
^
class file for android.hardware.display.DisplayManagerGlobal not found
/path/to/MyTest.java:222: error: cannot access EmojiFactory
transcript.add("onItemClick " + shadowOf(view).innerText() + ", " + position + ", " + id);
^
class file for android.emoji.EmojiFactory not found
/path/to/MyTest.java:227: error: cannot access GeoPoint
shadowOf(mView).performItemClick(2);
^
class file for com.google.android.maps.GeoPoint not found
/path/to/MyTest.java:239: error: cannot access ItemizedOverlay
transcript.add("onItemClick " + shadowOf(view).innerText() + ", " + position + ", " + id);
^
class file for com.google.android.maps.ItemizedOverlay not found
/path/to/MyTest.java:289: error: cannot access WindowManagerImpl
assertThat(Shadows.shadowOf(v.findViewById(R.id.ad_container)).innerText()).isEmpty();
^
class file for android.view.WindowManagerImpl not found
/path/to/MyTest.java:305: error: cannot access WindowManagerGlobal
assertThat(Shadows.shadowOf(v.findViewById(R.id.ad_container)).innerText()).isEqualTo("1, MREC_1, UNKNOWN");
^
class file for android.view.WindowManagerGlobal not found |
These tests compiled fine in robolectric 2.4, using
1
| Robolectric.shadowOf(...) |
. Is there something else that I missed in the 2.4-to-3.0 migration?
该提问来源于开源项目:robolectric/robolectric
Also, with the new unit test support in 1.1.0, Android is already adding the mockableAndroid.jar to the classpath, so we shouldn't be adding android-all anymore (only if using 1.1.0, of course).
Using the mockable android.jar is a completely separate thing from using Robolectric.