From 2538f131768f3ceb0978e8cd24aa59537610cfe6 Mon Sep 17 00:00:00 2001 From: pivovarit Date: Fri, 3 Mar 2017 08:32:46 +0100 Subject: [PATCH] Mockito2 Java8 refactor --- .../ArgumentMatcherWithoutLambdaUnitTest.java | 26 ++++++++----------- .../java8/CustomAnswerWithLambdaUnitTest.java | 21 ++++++++------- .../CustomAnswerWithoutLambdaUnitTest.java | 23 ++++++++-------- 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java index f4e8fb4cf5..786062ee57 100644 --- a/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/ArgumentMatcherWithoutLambdaUnitTest.java @@ -1,21 +1,18 @@ package com.baeldung.mockito.java8; +import org.junit.Before; +import org.junit.Test; +import org.mockito.*; + +import java.util.Optional; + import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.when; -import java.util.Optional; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentMatchers; -import org.mockito.InjectMocks; -import org.mockito.ArgumentMatcher; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; - public class ArgumentMatcherWithoutLambdaUnitTest { + @InjectMocks private UnemploymentServiceImpl unemploymentService; @@ -38,13 +35,12 @@ public class ArgumentMatcherWithoutLambdaUnitTest { } private class PeterArgumentMatcher implements ArgumentMatcher { + @Override public boolean matches(Person p) { - - if (p.getName().equals("Peter")) { - return true; - } - return false; + return p + .getName() + .equals("Peter"); } } diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java index b4e5abdc6c..06e9bca6d3 100644 --- a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithLambdaUnitTest.java @@ -1,19 +1,20 @@ package com.baeldung.mockito.java8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -import java.util.stream.Stream; - import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + public class CustomAnswerWithLambdaUnitTest { + @InjectMocks private UnemploymentServiceImpl unemploymentService; @@ -38,8 +39,8 @@ public class CustomAnswerWithLambdaUnitTest { public void init() { MockitoAnnotations.initMocks(this); - when(jobService.listJobs(any(Person.class))).then((i) -> { - return ((Person) i.getArgument(0)).getName().equals("Peter") ? Stream.of(new JobPosition("Teacher")) : Stream.empty(); - }); + when(jobService.listJobs(any(Person.class))).then((i) -> + Stream.of(new JobPosition("Teacher")) + .filter(p -> ((Person) i.getArgument(0)).getName().equals("Peter"))); } } diff --git a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java index 28922bb303..9d1aa3a3c0 100644 --- a/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java +++ b/mockito2/src/test/java/com/baeldung/mockito/java8/CustomAnswerWithoutLambdaUnitTest.java @@ -1,12 +1,5 @@ package com.baeldung.mockito.java8; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.when; - -import java.util.stream.Stream; - import org.junit.Before; import org.junit.Test; import org.mockito.InjectMocks; @@ -15,8 +8,16 @@ import org.mockito.MockitoAnnotations; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; +import java.util.stream.Stream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + public class CustomAnswerWithoutLambdaUnitTest { + @InjectMocks private UnemploymentServiceImpl unemploymentService; @@ -38,15 +39,13 @@ public class CustomAnswerWithoutLambdaUnitTest { } private class PersonAnswer implements Answer> { + @Override public Stream answer(InvocationOnMock invocation) throws Throwable { Person person = invocation.getArgument(0); - if(person.getName().equals("Peter")) { - return Stream.builder().add(new JobPosition("Teacher")).build(); - } - - return Stream.empty(); + return Stream.of(new JobPosition("Teacher")) + .filter(p -> person.getName().equals("Peter")); } }