使用Mockito在Android中运行的单元测试

时间:2019-03-14 09:52:34

标签: android mockito

如何编写单元测试来检查是否使用Mockito验证执行了chainOfTrustHelper.validate()?请帮忙!!!

像这样:verify(chainOfTrustHelper, atLeastOnce()).validate();

public class AutoInstallPieForegroundService extends LifecycleService {

    @Override
    public void onCreate() {
        super.onCreate();

        InertiaApplication.getAppComponent().inject(this);

        HandlerThread mHandlerThread = new HandlerThread("AutoInstallPieThread");
        mHandlerThread.start();

        Looper serviceLooper = mHandlerThread.getLooper();
        mHandler = new Handler(serviceLooper);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags, startId);
        startForeground(AutoInstallPieForegroundService.class.hashCode(), notification());
        doContainerWork();
        return START_NOT_STICKY;
    }

    public void doContainerWork() {
        if(appDao.getInstallCount() != null && appDao.getInstallCount().hasActiveObservers()) {
            appDao.getInstallCount().observe(AutoInstallPieForegroundService.this, new Observer<Integer>() {
                @Override
                public void onChanged(Integer installsRemaining) {
                    logger.d(TAG, "Observer 'installsRemaining' value is: " + installsRemaining);
                    if (installsRemaining == 0) {
                        stopSelf();
                    }
                }
            });
        }
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                Job.Result result = chainOfTrustHelper.validate();
                logger.d(TAG, result.name());
                switch (result) {
                    case RESCHEDULE:
                        AutoInstallPieKickoff.runAutoInstallPieKickOffWithDelay();
                        stopSelf();
                        break;
                    case SUCCESS:
                        if (provisionHelper.provision() == Job.Result.FAILURE) {
                            stopSelf();
                        }
                        break;
                }
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我通过在测试类中创建Handler并将其传递给Test Class来解决此问题:

public class AutoInstallPieForegroundServiceTest {

@Mock
private InertiaAppDao inertiaAppDao;
@Mock
private ChainOfTrustHelper chainOfTrustHelper;
@Mock
private ProvisionHelper provisionHelper;
@Mock
InertiaAppDataRepository inertiaAppDataRepository;
@Mock
private Handler mHandler;
@Mock
private Executor executor;
@Mock
private InertiaLogger inertiaLogger;

AutoInstallPieForegroundService autoInstallPieForegroundService;

@Before
public void setup() {
    MockitoAnnotations.initMocks(this);
    mHandler = spy(new Handler(getMainLooper()));
}

@Test
public void Should_CallProvisionHelper() {
    when(chainOfTrustHelper.validate()).thenReturn(Job.Result.SUCCESS);
    when(provisionHelper.provision()).thenReturn(Job.Result.SUCCESS);
    autoInstallPieForegroundService = new AutoInstallPieForegroundService( inertiaLogger, mHandler, chainOfTrustHelper,
            provisionHelper, inertiaAppDao);
    autoInstallPieForegroundService.doContainerWork();
    verify(provisionHelper, atLeastOnce()).provision();
}

@Test
public void Should_CallChainOfTrustHelper() {
    when(chainOfTrustHelper.validate()).thenReturn(Job.Result.RESCHEDULE);
    autoInstallPieForegroundService = new AutoInstallPieForegroundService( inertiaLogger, mHandler, chainOfTrustHelper,
            provisionHelper, inertiaAppDao);
    autoInstallPieForegroundService.doContainerWork();
    verify(chainOfTrustHelper, atLeastOnce()).validate();
}

@Test
public void Should_CallInertiaAppDao() {
    when(chainOfTrustHelper.validate()).thenReturn(Job.Result.RESCHEDULE);
    autoInstallPieForegroundService = new AutoInstallPieForegroundService( inertiaLogger, mHandler, chainOfTrustHelper,
            provisionHelper, inertiaAppDao);
    autoInstallPieForegroundService.doContainerWork();
    verify(inertiaAppDao, atLeastOnce()).getInstallCount();
}

}