我试图编写一个测试取消改装电话的单元测试,但是我被卡住了。
以下是实施:
public class LoginInteractorImpl extends AbstractInteractor implements LoginInteractor {
private volatile Call<ResponseBody> mCall;
private UserRepo mUserRepository;
@Inject
public LoginInteractorImpl(WorkerThread workerThread, MainThread mainThread, UserRepo userRepository) {
super(workerThread,mainThread);
this.mUserRepository = userRepository;
}
@Override
public void login(final String username, final String password, final OnLoginFinishedListener listener) {
mWorkerThread.execute(new Runnable() {
@Override
public void run() {
mCall = mUserRepository.doLogin(Credentials.basic(username, password));
enqueue(new LoginCallbackImpl(mWorkerThread, mMainThread, listener));
}
});
}
@Override
public void cancel() {
if(mCall != null) {
mCall.cancel();
}
}
void enqueue(LoginCallback callback){
mCall.enqueue(callback);
}
}
这是我到目前为止在我的测试类中所得到的:
public class LoginInteractorTest {
private LoginInteractorImpl mLoginInteractor;
@Mock
UserRepo mockUserRepository;
@Mock
private Call<ResponseBody> mockCall;
@Mock
private LoginInteractor.OnLoginFinishedListener mockLoginListener;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
mLoginInteractor = new LoginInteractorImpl(new FakeWorkerThread(), new FakeMainThread(), mockUserRepository);
}
...
@Test
public void shouldCancelLoginCall() throws Exception {
final String username = "test";
final String password = "testtest";
LoginInteractorImpl spy = Mockito.spy(mLoginInteractor);
when(mockUserRepository.doLogin(anyString()))
.thenReturn(mockCall);
final CountDownLatch latch = new CountDownLatch(1);
// Somehow delay the call and cancel it instead?
when(mLoginInteractor.enqueue(any(LoginCallback.class)))
.thenAnswer(
// Somehow pospone the call from executing
);
// Enqueue logincall
mLoginInteractor.login(username, password, mockLoginListener);
// Cancel call
mLoginInteractor.cancel();
// Verify that cancel was called
verify(mockCall, times(1)).cancel();
}
}
我的问题是如何阻止mockCall被执行并验证我是否成功取消了?我最好的选择是我不得不使用CountDownLatch,但我之前从未使用它,并且无法在任何地方找到并回答如何在我的用例中使用它。
答案 0 :(得分:0)
自己找到答案:
public void shouldCancelLoginCall() throws Exception {
final String username = "test";
final String password = "testtest";
LoginInteractorImpl spy = Mockito.spy(mLoginInteractor);
when(mockUserRepository.doLogin(anyString())).thenReturn(mockCall);
doNothing().when(spy).enqueue(any(LoginCallback.class));
mLoginInteractor.login(username, password, mockLoginListener);
mLoginInteractor.cancel();
verify(mockCall, times(1)).cancel();
}