From 0242d74b936e4c8e647761548f5925a29d07f11d Mon Sep 17 00:00:00 2001 From: Jonathan Cook Date: Sat, 23 Jun 2018 17:11:52 +0200 Subject: [PATCH] BAEL-1863 - Calling Callbacks with Mockito (#4531) * BAEL-1849 - Convert from String to Date in Java * BAEL-1863 - Calling Callbacks with Mockito --- .../mockito/service/ActionHandler.java | 26 ++++++++ .../baeldung/mockito/service/Callback.java | 6 ++ .../org/baeldung/mockito/service/Data.java | 15 +++++ .../baeldung/mockito/service/Response.java | 24 +++++++ .../org/baeldung/mockito/service/Service.java | 7 ++ .../service/ActionHandlerUnitTest.java | 65 +++++++++++++++++++ 6 files changed, 143 insertions(+) create mode 100644 testing-modules/mockito/src/main/java/org/baeldung/mockito/service/ActionHandler.java create mode 100644 testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Callback.java create mode 100644 testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Data.java create mode 100644 testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Response.java create mode 100644 testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Service.java create mode 100644 testing-modules/mockito/src/test/java/org/baeldung/mockito/service/ActionHandlerUnitTest.java diff --git a/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/ActionHandler.java b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/ActionHandler.java new file mode 100644 index 0000000000..289dcff399 --- /dev/null +++ b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/ActionHandler.java @@ -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() { + @Override + public void reply(Response response) { + handleResponse(response); + } + }); + } + + private void handleResponse(Response response) { + if (response.isValid()) { + response.setData(new Data("Successful data response")); + } + } + +} diff --git a/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Callback.java b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Callback.java new file mode 100644 index 0000000000..fb8d01ce2e --- /dev/null +++ b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Callback.java @@ -0,0 +1,6 @@ +package org.baeldung.mockito.service; + +public interface Callback { + + void reply(T response); +} diff --git a/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Data.java b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Data.java new file mode 100644 index 0000000000..665c05382c --- /dev/null +++ b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Data.java @@ -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; + } + +} diff --git a/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Response.java b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Response.java new file mode 100644 index 0000000000..22474a5ba7 --- /dev/null +++ b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Response.java @@ -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; + } + +} diff --git a/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Service.java b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Service.java new file mode 100644 index 0000000000..63434e53fb --- /dev/null +++ b/testing-modules/mockito/src/main/java/org/baeldung/mockito/service/Service.java @@ -0,0 +1,7 @@ +package org.baeldung.mockito.service; + +public interface Service { + + void doAction(String request, Callback callback); + +} diff --git a/testing-modules/mockito/src/test/java/org/baeldung/mockito/service/ActionHandlerUnitTest.java b/testing-modules/mockito/src/test/java/org/baeldung/mockito/service/ActionHandlerUnitTest.java new file mode 100644 index 0000000000..c34a9a4a09 --- /dev/null +++ b/testing-modules/mockito/src/test/java/org/baeldung/mockito/service/ActionHandlerUnitTest.java @@ -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> 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 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) invocation -> { + Callback 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(); + } +}