如何使用mockito避免使用onPause()?

时间:2015-10-30 20:05:16

标签: android unit-testing testing mockito

我有这个方法

@Override
protected void onPause() {
    unsuscribeFromMessageDispatcher(RaffleListActivity.this);
    Intent service = new Intent(this, AsyncIntentService.class);
    service.putExtra("TASK", TASKID_GET_RAFFLE_LIST);
    stopService(service);
    super.onPause();
}

当我进行测试时,如果在super.onPause();中失败,我怎么能避免使用mockito?

1 个答案:

答案 0 :(得分:1)

AFAIK, you cannot do this with Mockito. But you can with PowerMock:

@RunWith(PowerMockRunner.class)
@PrepareForTest(YourTestClass.class)
public class YourTest {

    @Test
    public void test() throws Exception {
        MemberModifier.suppress(MemberMatcher.method(YourParentClass.class, "onPause"));
        YourTestClass c = new YourTestClass();
          //super.onPause() will not be called here
        c.onPause();
    }