I am using the Volley library to make HTTP requests.
I want to make a test where I stub the HTTP response with WireMock. The WireMock stubbing part is not a problem but I can't think of a solution that allows me to wait for the async Volley request to finish.
@Test
public void callOnSuccessWhenGoodResponse()
{
// WireMock stubbing...
myVolleyClass.signup(infos, mockedCallback); // Async operation
// Have to wait here for the async operation to finish
Mockito.verify(mockedCallback).onSuccess(token);
}
How do I wait for the async operation to finish ?
I've tried with a CountDownLatch like that :
@Test
public void callOnSuccessWhenGoodResponse()
{
CountDownLatch latch = new CountDownLatch(1);
MyCallback callback = new MyCallBack()
{
@Override
void onSuccess()
{
latch.countDown();
}
void onError()
{
fail("Expected onSuccess but got onError");
}
};
// WireMock stubbing...
myVolleyClass.signup(infos, callback); // Async operation
latch.await(3, TimeUnit.SECONDS);
assertEquals(0, latch.getCount());
}
but the await() blocks the current thread and Volley executes the callback that we give it on the UI Thread which I guess would be the thread the test is running on. So the countdown() would never execute in time.
The best solution would be to have Volley make its requests synchronously during this test but I don't know if it's possible and what to mock. Any hints ?
Aucun commentaire:
Enregistrer un commentaire