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:
parent
550806ab32
commit
0242d74b93
|
@ -0,0 +1,26 @@
|
|||
package org.baeldung.mockito.service;
|
||||
|
||||
public class ActionHandler {
|
||||
|
||||
private Service service;
|
||||
|
||||
public ActionHandler(Service service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public void doAction() {
|
||||
service.doAction("our-request", new Callback<Response>() {
|
||||
@Override
|
||||
public void reply(Response response) {
|
||||
handleResponse(response);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void handleResponse(Response response) {
|
||||
if (response.isValid()) {
|
||||
response.setData(new Data("Successful data response"));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
package org.baeldung.mockito.service;
|
||||
|
||||
public interface Callback<T> {
|
||||
|
||||
void reply(T response);
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
package org.baeldung.mockito.service;
|
||||
|
||||
public class Data {
|
||||
|
||||
private String message;
|
||||
|
||||
public Data(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
package org.baeldung.mockito.service;
|
||||
|
||||
public class Response {
|
||||
|
||||
private Data data;
|
||||
private boolean isValid = true;
|
||||
|
||||
public boolean isValid() {
|
||||
return isValid;
|
||||
}
|
||||
|
||||
public void setIsValid(boolean isValid) {
|
||||
this.isValid = isValid;
|
||||
}
|
||||
|
||||
public void setData(Data data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public Data getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package org.baeldung.mockito.service;
|
||||
|
||||
public interface Service {
|
||||
|
||||
void doAction(String request, Callback<Response> callback);
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue