BAEL-1863 - Calling Callbacks with Mockito (#4531)

* BAEL-1849 - Convert from String to Date in Java

* BAEL-1863 - Calling Callbacks with Mockito
This commit is contained in:
Jonathan Cook
2018-06-23 17:11:52 +02:00
committed by maibin
parent 550806ab32
commit 0242d74b93
6 changed files with 143 additions and 0 deletions
@@ -0,0 +1,65 @@
package org.baeldung.mockito.service;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.verify;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.stubbing.Answer;
public class ActionHandlerUnitTest {
@Mock
private Service service;
@Captor
private ArgumentCaptor<Callback<Response>> callbackCaptor;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
}
@Test
public void givenServiceWithValidResponse_whenCallbackReceived_thenProcessed() {
ActionHandler handler = new ActionHandler(service);
handler.doAction();
verify(service).doAction(anyString(), callbackCaptor.capture());
Callback<Response> callback = callbackCaptor.getValue();
Response response = new Response();
callback.reply(response);
String expectedMessage = "Successful data response";
Data data = response.getData();
assertEquals("Should receive a successful message: ", expectedMessage, data.getMessage());
}
@Test
public void givenServiceWithInvalidResponse_whenCallbackReceived_thenNotProcessed() {
Response response = new Response();
response.setIsValid(false);
doAnswer((Answer<Void>) invocation -> {
Callback<Response> callback = invocation.getArgument(1);
callback.reply(response);
Data data = response.getData();
assertNull("No data in invalid response: ", data);
return null;
}).when(service)
.doAction(anyString(), any(Callback.class));
ActionHandler handler = new ActionHandler(service);
handler.doAction();
}
}