Skip to content

Commit 0242d74

Browse files
JonCookmaibin
authored andcommitted
BAEL-1863 - Calling Callbacks with Mockito (eugenp#4531)
* BAEL-1849 - Convert from String to Date in Java * BAEL-1863 - Calling Callbacks with Mockito
1 parent 550806a commit 0242d74

6 files changed

Lines changed: 143 additions & 0 deletions

File tree

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.baeldung.mockito.service;
2+
3+
public class ActionHandler {
4+
5+
private Service service;
6+
7+
public ActionHandler(Service service) {
8+
this.service = service;
9+
}
10+
11+
public void doAction() {
12+
service.doAction("our-request", new Callback<Response>() {
13+
@Override
14+
public void reply(Response response) {
15+
handleResponse(response);
16+
}
17+
});
18+
}
19+
20+
private void handleResponse(Response response) {
21+
if (response.isValid()) {
22+
response.setData(new Data("Successful data response"));
23+
}
24+
}
25+
26+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.baeldung.mockito.service;
2+
3+
public interface Callback<T> {
4+
5+
void reply(T response);
6+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.baeldung.mockito.service;
2+
3+
public class Data {
4+
5+
private String message;
6+
7+
public Data(String message) {
8+
this.message = message;
9+
}
10+
11+
public String getMessage() {
12+
return message;
13+
}
14+
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.mockito.service;
2+
3+
public class Response {
4+
5+
private Data data;
6+
private boolean isValid = true;
7+
8+
public boolean isValid() {
9+
return isValid;
10+
}
11+
12+
public void setIsValid(boolean isValid) {
13+
this.isValid = isValid;
14+
}
15+
16+
public void setData(Data data) {
17+
this.data = data;
18+
}
19+
20+
public Data getData() {
21+
return data;
22+
}
23+
24+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.baeldung.mockito.service;
2+
3+
public interface Service {
4+
5+
void doAction(String request, Callback<Response> callback);
6+
7+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.baeldung.mockito.service;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertNull;
5+
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.ArgumentMatchers.anyString;
7+
import static org.mockito.Mockito.doAnswer;
8+
import static org.mockito.Mockito.verify;
9+
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.mockito.ArgumentCaptor;
13+
import org.mockito.Captor;
14+
import org.mockito.Mock;
15+
import org.mockito.MockitoAnnotations;
16+
import org.mockito.stubbing.Answer;
17+
18+
public class ActionHandlerUnitTest {
19+
20+
@Mock
21+
private Service service;
22+
23+
@Captor
24+
private ArgumentCaptor<Callback<Response>> callbackCaptor;
25+
26+
@Before
27+
public void setup() {
28+
MockitoAnnotations.initMocks(this);
29+
}
30+
31+
@Test
32+
public void givenServiceWithValidResponse_whenCallbackReceived_thenProcessed() {
33+
ActionHandler handler = new ActionHandler(service);
34+
handler.doAction();
35+
36+
verify(service).doAction(anyString(), callbackCaptor.capture());
37+
38+
Callback<Response> callback = callbackCaptor.getValue();
39+
Response response = new Response();
40+
callback.reply(response);
41+
42+
String expectedMessage = "Successful data response";
43+
Data data = response.getData();
44+
assertEquals("Should receive a successful message: ", expectedMessage, data.getMessage());
45+
}
46+
47+
@Test
48+
public void givenServiceWithInvalidResponse_whenCallbackReceived_thenNotProcessed() {
49+
Response response = new Response();
50+
response.setIsValid(false);
51+
52+
doAnswer((Answer<Void>) invocation -> {
53+
Callback<Response> callback = invocation.getArgument(1);
54+
callback.reply(response);
55+
56+
Data data = response.getData();
57+
assertNull("No data in invalid response: ", data);
58+
return null;
59+
}).when(service)
60+
.doAction(anyString(), any(Callback.class));
61+
62+
ActionHandler handler = new ActionHandler(service);
63+
handler.doAction();
64+
}
65+
}

0 commit comments

Comments
 (0)