Android Question

Mockito anyBoolean() matcher exception on return

Trying to write a simple unit test with Mockito:

@Test
public void toggleBlockingControlTest_turnOff() {
when(mainViewPresenter.getAppSettingsStorage().loadBlockingStatus())
.thenReturn(Mockito.anyBoolean());

AppSettingsStorage appSettingsStorage = mainViewPresenter.getAppSettingsStorage();
boolean statusBefore = appSettingsStorage.loadBlockingStatus();

Mockito.verify(appSettingsStorage).saveBlockingStatus(Mockito.eq(!statusBefore));
}


In runtime i got org.mockito.exceptions.misusing.InvalidUseOfMatchersException: at this string:

.thenReturn(Mockito.anyBoolean());


I am new to Mockito and can't understand why this framework cant return any boolean???

Answer
when(mainViewPresenter.getAppSettingsStorage().loadBlockingStatus())
                .thenReturn(Mockito.anyBoolean());

Here you should return either Boolean.TRUE or Boolean.FALSE. And If you don't care about this boolean value and remove this line from your test case.

Replace: Mockito.anyBoolean() --> Boolean.TRUE /Boolean.FALSE

Comments