From b9b642c6a9b2c05ac8f663b8c0b882a9ac14e2e3 Mon Sep 17 00:00:00 2001 From: Yasin Date: Sat, 27 May 2017 01:47:42 +0530 Subject: [PATCH] BAEL-900 Guide to dynamic tests in Junit 5 (#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 d0d45c9067223347da20d0f2c80de391fcade38e. * 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 --- .../com/baeldung/DynamicTestsExample.java | 26 +++++++++++++ .../java/com/baeldung/helpers/Employee.java | 38 +++++++++++++++++++ .../com/baeldung/helpers/EmployeeDao.java | 16 ++++++++ 3 files changed, 80 insertions(+) create mode 100644 junit5/src/test/java/com/baeldung/helpers/Employee.java create mode 100644 junit5/src/test/java/com/baeldung/helpers/EmployeeDao.java diff --git a/junit5/src/test/java/com/baeldung/DynamicTestsExample.java b/junit5/src/test/java/com/baeldung/DynamicTestsExample.java index ce1f8b16a7..6a97f2347b 100644 --- a/junit5/src/test/java/com/baeldung/DynamicTestsExample.java +++ b/junit5/src/test/java/com/baeldung/DynamicTestsExample.java @@ -18,6 +18,9 @@ import org.junit.jupiter.api.DynamicTest; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.function.ThrowingConsumer; +import com.baeldung.helpers.Employee; +import com.baeldung.helpers.EmployeeDao; + public class DynamicTestsExample { @TestFactory @@ -111,6 +114,29 @@ public class DynamicTestsExample { } + @TestFactory + Stream dynamicTestsForEmployeeWorkflows() { + List inputList = + Arrays.asList(new Employee(1, "Fred"), new Employee(2), new Employee(3, "John")); + + EmployeeDao dao = new EmployeeDao(); + Stream saveEmployeeStream = inputList.stream().map(emp -> + DynamicTest.dynamicTest("saveEmployee: " + emp.toString(), () -> { + Employee returned = dao.save(emp.getId()); + assertEquals(returned.getId(), emp.getId()); + })); + + Stream saveEmployeeWithFirstNameStream + = inputList.stream().filter(emp -> !emp.getFirstName().isEmpty()) + .map(emp -> DynamicTest.dynamicTest("saveEmployeeWithName" + emp.toString(), () -> { + Employee returned = dao.save(emp.getId(), emp.getFirstName()); + assertEquals(returned.getId(), emp.getId()); + assertEquals(returned.getFirstName(), emp.getFirstName()); + })); + + return Stream.concat(saveEmployeeStream, saveEmployeeWithFirstNameStream); + } + class DomainNameResolver { private Map ipByDomainName = new HashMap<>(); diff --git a/junit5/src/test/java/com/baeldung/helpers/Employee.java b/junit5/src/test/java/com/baeldung/helpers/Employee.java new file mode 100644 index 0000000000..7fa724e4a8 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/helpers/Employee.java @@ -0,0 +1,38 @@ +package com.baeldung.helpers; + +public class Employee { + + private long id; + private String firstName; + + public Employee(long id) { + this.id = id; + this.firstName = ""; + } + + public Employee(long id, String firstName) { + this.id = id; + this.firstName = firstName; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + @Override + public String toString() { + return "Employee [id=" + id + ", firstName=" + firstName + "]"; + } +} diff --git a/junit5/src/test/java/com/baeldung/helpers/EmployeeDao.java b/junit5/src/test/java/com/baeldung/helpers/EmployeeDao.java new file mode 100644 index 0000000000..b23e5bf5e3 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/helpers/EmployeeDao.java @@ -0,0 +1,16 @@ +package com.baeldung.helpers; + +public class EmployeeDao { + + public Employee save(long id) { + return new Employee(id); + } + + public Employee save(long id, String firstName) { + return new Employee(id, firstName); + } + + public Employee update(Employee employee) { + return employee; + } +}