From 43a31ff5e7206497ff27e2b9f68931210e134e3f Mon Sep 17 00:00:00 2001 From: MewX Date: Sun, 4 Feb 2018 16:20:31 +1030 Subject: [PATCH] Finished normal Mockito testing codes. --- .../practice/javamockito/ServiceCaller.java | 4 +- .../javamockito/ServiceCallerTest.java | 55 +++++++++++++++++++ 2 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 JustPractice/java/javamockito/src/test/org/mewx/practice/javamockito/ServiceCallerTest.java diff --git a/JustPractice/java/javamockito/src/main/org/mewx/practice/javamockito/ServiceCaller.java b/JustPractice/java/javamockito/src/main/org/mewx/practice/javamockito/ServiceCaller.java index f54d3f1..f0ea68d 100644 --- a/JustPractice/java/javamockito/src/main/org/mewx/practice/javamockito/ServiceCaller.java +++ b/JustPractice/java/javamockito/src/main/org/mewx/practice/javamockito/ServiceCaller.java @@ -3,7 +3,9 @@ import java.util.List; public class ServiceCaller { + private Service service = new Service(); + public void callService(List list, Message messageHost) { - new Service().runService(list, messageHost); + service.runService(list, messageHost); } } diff --git a/JustPractice/java/javamockito/src/test/org/mewx/practice/javamockito/ServiceCallerTest.java b/JustPractice/java/javamockito/src/test/org/mewx/practice/javamockito/ServiceCallerTest.java new file mode 100644 index 0000000..2a3780c --- /dev/null +++ b/JustPractice/java/javamockito/src/test/org/mewx/practice/javamockito/ServiceCallerTest.java @@ -0,0 +1,55 @@ +package org.mewx.practice.javamockito; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.*; +import org.mockito.runners.MockitoJUnitRunner; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +@RunWith(MockitoJUnitRunner.class) +public class ServiceCallerTest { + @InjectMocks + private ServiceCaller serviceCaller = new ServiceCaller(); + @Spy + private Service serviceInside = new Service(); // NOTE: cannot apply to new Service() objects inside methods // TODO: try PowerMockito + @Captor + private ArgumentCaptor> argCaptor; + + private Message message; + private ArrayList list; + + @Before + public void setUp() { + // Note: for JUnit 4.5+, no need to initMocks() any more + message = new Message(); + list = new ArrayList<>(); + list.add("Initial item"); + } + + @Test + public void callServiceWithOneInList() { + serviceCaller.callService(list, message); + verify(serviceInside, times(1)).runService(argCaptor.capture(), eq(message)); + assertEquals(1, argCaptor.getValue().size()); + assertEquals(1, message.getSize()); + assertEquals(Service.ERROR_MSG, message.getMessageAt(0)); + } + + @Test + public void callServiceWithTwoInList() { + list.add("Another item"); + + serviceCaller.callService(list, message); + verify(serviceInside, times(1)).runService(argCaptor.capture(), eq(message)); + assertEquals(2, argCaptor.getValue().size()); + assertEquals(0, message.getSize()); + } +} \ No newline at end of file