Mockito2 Java8 refactor

This commit is contained in:
pivovarit 2017-03-03 08:32:46 +01:00
parent 6d63084fc1
commit 2538f13176
3 changed files with 33 additions and 37 deletions

View File

@ -1,21 +1,18 @@
package com.baeldung.mockito.java8; 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.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when; 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 { public class ArgumentMatcherWithoutLambdaUnitTest {
@InjectMocks @InjectMocks
private UnemploymentServiceImpl unemploymentService; private UnemploymentServiceImpl unemploymentService;
@ -38,13 +35,12 @@ public class ArgumentMatcherWithoutLambdaUnitTest {
} }
private class PeterArgumentMatcher implements ArgumentMatcher<Person> { private class PeterArgumentMatcher implements ArgumentMatcher<Person> {
@Override @Override
public boolean matches(Person p) { public boolean matches(Person p) {
return p
if (p.getName().equals("Peter")) { .getName()
return true; .equals("Peter");
}
return false;
} }
} }

View File

@ -1,19 +1,20 @@
package com.baeldung.mockito.java8; 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.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; 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 { public class CustomAnswerWithLambdaUnitTest {
@InjectMocks @InjectMocks
private UnemploymentServiceImpl unemploymentService; private UnemploymentServiceImpl unemploymentService;
@ -38,8 +39,8 @@ public class CustomAnswerWithLambdaUnitTest {
public void init() { public void init() {
MockitoAnnotations.initMocks(this); MockitoAnnotations.initMocks(this);
when(jobService.listJobs(any(Person.class))).then((i) -> { when(jobService.listJobs(any(Person.class))).then((i) ->
return ((Person) i.getArgument(0)).getName().equals("Peter") ? Stream.of(new JobPosition("Teacher")) : Stream.empty(); Stream.of(new JobPosition("Teacher"))
}); .filter(p -> ((Person) i.getArgument(0)).getName().equals("Peter")));
} }
} }

View File

@ -1,12 +1,5 @@
package com.baeldung.mockito.java8; 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.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
@ -15,8 +8,16 @@ import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; 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 { public class CustomAnswerWithoutLambdaUnitTest {
@InjectMocks @InjectMocks
private UnemploymentServiceImpl unemploymentService; private UnemploymentServiceImpl unemploymentService;
@ -38,15 +39,13 @@ public class CustomAnswerWithoutLambdaUnitTest {
} }
private class PersonAnswer implements Answer<Stream<JobPosition>> { private class PersonAnswer implements Answer<Stream<JobPosition>> {
@Override @Override
public Stream<JobPosition> answer(InvocationOnMock invocation) throws Throwable { public Stream<JobPosition> answer(InvocationOnMock invocation) throws Throwable {
Person person = invocation.getArgument(0); Person person = invocation.getArgument(0);
if(person.getName().equals("Peter")) { return Stream.of(new JobPosition("Teacher"))
return Stream.<JobPosition>builder().add(new JobPosition("Teacher")).build(); .filter(p -> person.getName().equals("Peter"));
}
return Stream.empty();
} }
} }