Mockito2 Java8 refactor
This commit is contained in:
parent
6d63084fc1
commit
2538f13176
|
@ -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<Person> {
|
||||
|
||||
@Override
|
||||
public boolean matches(Person p) {
|
||||
|
||||
if (p.getName().equals("Peter")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return p
|
||||
.getName()
|
||||
.equals("Peter");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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")));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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<Stream<JobPosition>> {
|
||||
|
||||
@Override
|
||||
public Stream<JobPosition> answer(InvocationOnMock invocation) throws Throwable {
|
||||
Person person = invocation.getArgument(0);
|
||||
|
||||
if(person.getName().equals("Peter")) {
|
||||
return Stream.<JobPosition>builder().add(new JobPosition("Teacher")).build();
|
||||
}
|
||||
|
||||
return Stream.empty();
|
||||
return Stream.of(new JobPosition("Teacher"))
|
||||
.filter(p -> person.getName().equals("Peter"));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue