无法窥探ActivityInstrumentationTestCase2中的Android活动

时间:2015-06-17 08:50:31

标签: android testing android-activity mockito

我试图窥探Activity,但它给了我以下例外:

java.lang.AbstractMethodError: abstract method "boolean org.mockito.internal.invocation.AbstractAwareMethod.isAbstract()"
at org.mockito.internal.invocation.InvocationImpl.callRealMethod(InvocationImpl.java:109)
at org.mockito.internal.stubbing.answers.CallsRealMethods.answer(CallsRealMethods.java:41)
at org.mockito.internal.handler.MockHandlerImpl.handle(MockHandlerImpl.java:93)
at org.mockito.internal.handler.NullResultGuardian.handle(NullResultGuardian.java:29)
at org.mockito.internal.handler.InvocationNotifierHandler.handle(InvocationNotifierHandler.java:38)
at com.google.dexmaker.mockito.InvocationHandlerAdapter.invoke(InvocationHandlerAdapter.java:49)
at WaitForCardActivity_Proxy.isRegisterActivated(WaitForCardActivity_Proxy.generated)

代码如下:

@RunWith(AndroidJUnit4.class)
public class WaitForCardActivityTests extends ActivityInstrumentationTestCase2<WaitForCardActivity> {

    @Before
    @Override
    public void setUp() throws Exception {
        super.setUp();

        injectInstrumentation(InstrumentationRegistry.getInstrumentation());

        waitForCardActivity = spy(getActivity());
        when(waitForCardActivity.isRegisterActivated()).thenReturn(true);
    }

    ...
}

的活动:

public class WaitForCardActivity extends Activity {
    ...
    public boolean isRegisterActivated() {
        ...
    }
}

还要注意:我不能使用Robolectric,因为我使用的Cipher在运行Robolectric时会给我带来麻烦。

1 个答案:

答案 0 :(得分:0)

以下列方式监视Activity

Activity activity = spy(Activity.class);

<强>更新 在你的情况下:

    WaitForCardActivity activity = spy(WaitForCardActivity.class);
    when(activity.isRegisterActivated()).thenReturn(true);
    assertTrue(activity.isRegisterActivated());