Skip to content

Commit

Permalink
Mockito2 Java8 refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
pivovarit committed Mar 3, 2017
1 parent 6d63084 commit 2538f13
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
package com.baeldung.mockito.java8;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;
import org.junit.Before;
import org.junit.Test;
import org.mockito.*;

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;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;


public class ArgumentMatcherWithoutLambdaUnitTest {

@InjectMocks
private UnemploymentServiceImpl unemploymentService;

Expand All @@ -38,13 +35,12 @@ public void whenPersonWithJob_thenIsNotEntitled() {
}

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");
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -38,8 +39,8 @@ public void whenPersonWithNoJobHistory_thenSearchReturnsEmpty() {
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")));
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -15,8 +8,16 @@
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;

Expand All @@ -38,15 +39,13 @@ public void whenPersonWithNoJobHistory_thenSearchReturnsEmpty() {
}

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"));
}
}

Expand Down

0 comments on commit 2538f13

Please sign in to comment.