Skip to content

Commit b9b642c

Browse files
yasin3061maibin
authored andcommitted
BAEL-900 Guide to dynamic tests in Junit 5 (eugenp#1932)
* BAEL-900 Guide to dynamic tests in Junit 5 * BAEL-900 Guide to Dynamic Tests in Junit 5 * Revert "BAEL-900 Guide to Dynamic Tests in Junit 5" This reverts commit d0d45c9. * BAEL-900 Guide to Dynamic Tests in Junit 5 * BAEL-900 Guide to dynamic tests in Junit 5 * removed unnecessary annotation * BAEL-900 unused imports removed
1 parent 49724d1 commit b9b642c

3 files changed

Lines changed: 80 additions & 0 deletions

File tree

‎junit5/src/test/java/com/baeldung/DynamicTestsExample.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.junit.jupiter.api.TestFactory;
1919
import org.junit.jupiter.api.function.ThrowingConsumer;
2020

21+
import com.baeldung.helpers.Employee;
22+
import com.baeldung.helpers.EmployeeDao;
23+
2124
public class DynamicTestsExample {
2225

2326
@TestFactory
@@ -111,6 +114,29 @@ Stream<DynamicTest> dynamicTestsFromStreamInJava8() {
111114

112115
}
113116

117+
@TestFactory
118+
Stream<DynamicTest> dynamicTestsForEmployeeWorkflows() {
119+
List<Employee> inputList =
120+
Arrays.asList(new Employee(1, "Fred"), new Employee(2), new Employee(3, "John"));
121+
122+
EmployeeDao dao = new EmployeeDao();
123+
Stream<DynamicTest> saveEmployeeStream = inputList.stream().map(emp ->
124+
DynamicTest.dynamicTest("saveEmployee: " + emp.toString(), () -> {
125+
Employee returned = dao.save(emp.getId());
126+
assertEquals(returned.getId(), emp.getId());
127+
}));
128+
129+
Stream<DynamicTest> saveEmployeeWithFirstNameStream
130+
= inputList.stream().filter(emp -> !emp.getFirstName().isEmpty())
131+
.map(emp -> DynamicTest.dynamicTest("saveEmployeeWithName" + emp.toString(), () -> {
132+
Employee returned = dao.save(emp.getId(), emp.getFirstName());
133+
assertEquals(returned.getId(), emp.getId());
134+
assertEquals(returned.getFirstName(), emp.getFirstName());
135+
}));
136+
137+
return Stream.concat(saveEmployeeStream, saveEmployeeWithFirstNameStream);
138+
}
139+
114140
class DomainNameResolver {
115141

116142
private Map<String, String> ipByDomainName = new HashMap<>();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.helpers;
2+
3+
public class Employee {
4+
5+
private long id;
6+
private String firstName;
7+
8+
public Employee(long id) {
9+
this.id = id;
10+
this.firstName = "";
11+
}
12+
13+
public Employee(long id, String firstName) {
14+
this.id = id;
15+
this.firstName = firstName;
16+
}
17+
18+
public long getId() {
19+
return id;
20+
}
21+
22+
public void setId(long id) {
23+
this.id = id;
24+
}
25+
26+
public String getFirstName() {
27+
return firstName;
28+
}
29+
30+
public void setFirstName(String firstName) {
31+
this.firstName = firstName;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return "Employee [id=" + id + ", firstName=" + firstName + "]";
37+
}
38+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung.helpers;
2+
3+
public class EmployeeDao {
4+
5+
public Employee save(long id) {
6+
return new Employee(id);
7+
}
8+
9+
public Employee save(long id, String firstName) {
10+
return new Employee(id, firstName);
11+
}
12+
13+
public Employee update(Employee employee) {
14+
return employee;
15+
}
16+
}

0 commit comments

Comments
 (0)