我开始在Android Studio中使用Robolectric.起初我想用hamcrest创建一个简单的测试,如下所示:
@RunWith(CustomTestRunner.class)
@Config(emulateSdk = 18)
public class MainActivityTest {
private MainActivity mainActivity;
@Test
public void testMainActivity() {
mainActivity = buildActivity(MainActivity.class).create().get();
assertThat(mainActivity, notNullValue());
}
}
Run Code Online (Sandbox Code Playgroud)
执行测试时会抛出以下异常:
java.lang.ClassCastException: java.lang.NoSuchMethodError cannot be cast to java.lang.RuntimeException
at org.robolectric.internal.ReflectionHelpers.callInstanceMethodReflectively(ReflectionHelpers.java:68)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:115)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:268)
at org.robolectric.util.ActivityController.create(ActivityController.java:111)
at org.robolectric.util.ActivityController.create(ActivityController.java:122)
at com.enprodo.wakemethere.googleservices.geofence.GeofenceTest.testReceiver(GeofenceTest.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158) …
Run Code Online (Sandbox Code Playgroud) android robolectric android-studio robolectric-gradle-plugin
我很高兴听到 android studio 做了一些更新,使单元测试在 android studio 中变得简单,所以我一直在尝试设置 roboelectric,但我无法克服在以下方法中发生的这个错误。
MainActivity = Robolectric.buildActivity(MainActivity.class).create().get();
Run Code Online (Sandbox Code Playgroud)
使用以下堆栈跟踪
引起:java.lang.RuntimeException: java.lang.NoSuchMethodException: java.lang.Object.attach(android.content.Context, android.app.ActivityThread, android.app.Instrumentation, android.os.IBinder, int, android .app.Application, android.content.Intent, android.content.pm.ActivityInfo, java.lang.CharSequence, android.app.Activity, java.lang.String, android.app.Activity$NonConfigurationInstances, android.content.res .Configuration) 在 org.robolectric.util.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:248) 在 org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:138) ... 36 更多
为什么会发生这种情况以及我可以做些什么来克服它?