Skip to content

Commit b0e7410

Browse files
authored
Merge pull request eugenp#1279 from felipe-gdr/master
BAEL-632: Mockito's Java 8 features
2 parents beaf599 + 30a6924 commit b0e7410

15 files changed

Lines changed: 532 additions & 0 deletions

‎mockito2/.gitignore‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.class
2+
3+
.settings
4+
.project
5+
6+
#folders#
7+
/target
8+
/src/main/webapp/WEB-INF/classes
9+
*/META-INF/*
10+
11+
# Packaged files #
12+
*.jar
13+
*.war
14+
*.ear

‎mockito2/README.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=========
2+
3+
## Mockito 2 and Java 8 Tips
4+
5+
Examples on how to leverage Java 8 new features with Mockito version 2

‎mockito2/pom.xml‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>com.baeldung</groupId>
6+
<artifactId>mockito2</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
<name>mockito-2-with-java8</name>
10+
11+
<dependencies>
12+
<!-- test scoped -->
13+
<dependency>
14+
<groupId>junit</groupId>
15+
<artifactId>junit</artifactId>
16+
<version>${junit.version}</version>
17+
<scope>test</scope>
18+
</dependency>
19+
20+
<dependency>
21+
<groupId>org.mockito</groupId>
22+
<artifactId>mockito-core</artifactId>
23+
<version>${mockito.version}</version>
24+
<scope>test</scope>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<plugins>
30+
<plugin>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<version>${maven-compiler-plugin.version}</version>
33+
<configuration>
34+
<source>1.8</source>
35+
<target>1.8</target>
36+
</configuration>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.apache.maven.plugins</groupId>
40+
<artifactId>maven-surefire-plugin</artifactId>
41+
<version>${maven-surefire-plugin.version}</version>
42+
<configuration>
43+
<excludes>
44+
<exclude>**/*IntegrationTest.java</exclude>
45+
<exclude>**/*LiveTest.java</exclude>
46+
</excludes>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
52+
<profiles>
53+
<profile>
54+
<id>integration</id>
55+
<build>
56+
<plugins>
57+
<plugin>
58+
<groupId>org.apache.maven.plugins</groupId>
59+
<artifactId>maven-surefire-plugin</artifactId>
60+
<executions>
61+
<execution>
62+
<phase>integration-test</phase>
63+
<goals>
64+
<goal>test</goal>
65+
</goals>
66+
<configuration>
67+
<excludes>
68+
<exclude>**/*LiveTest.java</exclude>
69+
</excludes>
70+
<includes>
71+
<include>**/*IntegrationTest.java</include>
72+
</includes>
73+
</configuration>
74+
</execution>
75+
</executions>
76+
<configuration>
77+
<systemPropertyVariables>
78+
<test.mime>json</test.mime>
79+
</systemPropertyVariables>
80+
</configuration>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
</profile>
85+
</profiles>
86+
87+
<properties>
88+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
89+
90+
<!-- testing -->
91+
<junit.version>4.12</junit.version>
92+
<mockito.version>2.7.5</mockito.version>
93+
94+
<!-- maven plugins -->
95+
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
96+
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
97+
</properties>
98+
</project>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.mockito.java8;
2+
3+
public class JobPosition {
4+
private String title;
5+
6+
public JobPosition() {}
7+
8+
public JobPosition(String title) {
9+
super();
10+
this.title = title;
11+
}
12+
13+
public String getTitle() {
14+
return title;
15+
}
16+
17+
public void setTitle(String title) {
18+
this.title = title;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung.mockito.java8;
2+
3+
import java.util.Optional;
4+
import java.util.stream.Stream;
5+
6+
public interface JobService {
7+
Optional<JobPosition> findCurrentJobPosition(Person person);
8+
9+
default boolean assignJobPosition(Person person, JobPosition jobPosition) {
10+
if (!findCurrentJobPosition(person).isPresent()) {
11+
person.setCurrentJobPosition(jobPosition);
12+
13+
return true;
14+
} else {
15+
return false;
16+
}
17+
}
18+
19+
Stream<JobPosition> listJobs(Person person);
20+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.mockito.java8;
2+
3+
public class Person {
4+
private String name;
5+
private JobPosition currentJobPosition;
6+
7+
public Person() {}
8+
9+
public Person(String name) {
10+
this.name = name;
11+
}
12+
13+
public JobPosition getCurrentJobPosition() {
14+
return currentJobPosition;
15+
}
16+
17+
public void setCurrentJobPosition(JobPosition currentJobPosition) {
18+
this.currentJobPosition = currentJobPosition;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.mockito.java8;
2+
3+
import java.util.Optional;
4+
5+
public interface UnemploymentService {
6+
7+
boolean personIsEntitledToUnemploymentSupport(Person person);
8+
9+
Optional<JobPosition> searchJob(Person person, String searchString);
10+
11+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.mockito.java8;
2+
3+
import java.util.Optional;
4+
import java.util.stream.Stream;
5+
6+
public class UnemploymentServiceImpl implements UnemploymentService {
7+
private final JobService jobService;
8+
9+
public UnemploymentServiceImpl(JobService jobService) {
10+
this.jobService = jobService;
11+
}
12+
13+
@Override
14+
public boolean personIsEntitledToUnemploymentSupport(Person person) {
15+
Optional<JobPosition> optional = jobService.findCurrentJobPosition(person);
16+
17+
return !optional.isPresent();
18+
}
19+
20+
@Override
21+
public Optional<JobPosition> searchJob(Person person, String searchString) {
22+
Stream<JobPosition> stream = jobService.listJobs(person);
23+
24+
return stream.filter((j) -> j.getTitle().contains(searchString)).findFirst();
25+
}
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.mockito.java8;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.when;
6+
7+
import java.util.Optional;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.mockito.ArgumentMatchers;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.Mock;
14+
import org.mockito.MockitoAnnotations;
15+
16+
17+
public class ArgumentMatcherWithLambdaUnitTest {
18+
19+
@InjectMocks
20+
private UnemploymentServiceImpl unemploymentService;
21+
22+
@Mock
23+
private JobService jobService;
24+
25+
@Test
26+
public void whenPersonWithJob_thenIsNotEntitled() {
27+
Person peter = new Person("Peter");
28+
Person linda = new Person("Linda");
29+
30+
JobPosition teacher = new JobPosition("Teacher");
31+
32+
when(jobService.findCurrentJobPosition(
33+
ArgumentMatchers.argThat((p) -> p.getName().equals("Peter")))
34+
).thenReturn(Optional.of(teacher));
35+
36+
assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(linda));
37+
assertFalse(unemploymentService.personIsEntitledToUnemploymentSupport(peter));
38+
}
39+
40+
@Before
41+
public void init() {
42+
MockitoAnnotations.initMocks(this);
43+
}
44+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.baeldung.mockito.java8;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertTrue;
5+
import static org.mockito.Mockito.when;
6+
7+
import java.util.Optional;
8+
9+
import org.junit.Before;
10+
import org.junit.Test;
11+
import org.mockito.ArgumentMatchers;
12+
import org.mockito.InjectMocks;
13+
import org.mockito.ArgumentMatcher;
14+
import org.mockito.Mock;
15+
import org.mockito.MockitoAnnotations;
16+
17+
18+
public class ArgumentMatcherWithoutLambdaUnitTest {
19+
@InjectMocks
20+
private UnemploymentServiceImpl unemploymentService;
21+
22+
@Mock
23+
private JobService jobService;
24+
25+
@Test
26+
public void whenPersonWithJob_thenIsNotEntitled() {
27+
Person peter = new Person("Peter");
28+
Person linda = new Person("Linda");
29+
30+
JobPosition teacher = new JobPosition("Teacher");
31+
32+
when(jobService.findCurrentJobPosition(
33+
ArgumentMatchers.argThat(new PeterArgumentMatcher()))
34+
).thenReturn(Optional.of(teacher));
35+
36+
assertTrue(unemploymentService.personIsEntitledToUnemploymentSupport(linda));
37+
assertFalse(unemploymentService.personIsEntitledToUnemploymentSupport(peter));
38+
}
39+
40+
private class PeterArgumentMatcher implements ArgumentMatcher<Person> {
41+
@Override
42+
public boolean matches(Person p) {
43+
44+
if (p.getName().equals("Peter")) {
45+
return true;
46+
}
47+
return false;
48+
}
49+
}
50+
51+
@Before
52+
public void init() {
53+
MockitoAnnotations.initMocks(this);
54+
}
55+
}

0 commit comments

Comments
 (0)