From 86ab50684b1d464b5adfdfbfcc3e3d201455151e Mon Sep 17 00:00:00 2001 From: Trixi Turny Date: Thu, 4 Nov 2021 18:15:45 +0000 Subject: [PATCH 1/2] BAEL-5209 example with JUnit5, Hamcrest and AssertJ --- .../com/baeldung/object/type/Deciduous.java | 15 ++++++++++ .../com/baeldung/object/type/Evergreen.java | 16 +++++++++++ .../java/com/baeldung/object/type/Tree.java | 6 ++++ .../com/baeldung/object/type/TreeSorter.java | 19 +++++++++++++ .../type/TreeSorterAssertJUnitTest.java | 22 +++++++++++++++ .../type/TreeSorterHamcrestUnitTest.java | 28 +++++++++++++++++++ 6 files changed, 106 insertions(+) create mode 100644 testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Deciduous.java create mode 100644 testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Evergreen.java create mode 100644 testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Tree.java create mode 100644 testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/TreeSorter.java create mode 100644 testing-modules/testing-assertions/src/test/java/com/baeldung/object/type/TreeSorterAssertJUnitTest.java create mode 100644 testing-modules/testing-assertions/src/test/java/com/baeldung/object/type/TreeSorterHamcrestUnitTest.java diff --git a/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Deciduous.java b/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Deciduous.java new file mode 100644 index 0000000000..ae96446a63 --- /dev/null +++ b/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Deciduous.java @@ -0,0 +1,15 @@ +package com.baeldung.object.type; + +public class Deciduous implements Tree { + + private String name; + + public Deciduous(String name) { + this.name = name; + } + + @Override + public boolean isEvergreen() { + return false; + } +} diff --git a/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Evergreen.java b/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Evergreen.java new file mode 100644 index 0000000000..56cda60d22 --- /dev/null +++ b/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Evergreen.java @@ -0,0 +1,16 @@ +package com.baeldung.object.type; + +public class Evergreen implements Tree { + + private String name; + + public Evergreen(String name) { + this.name = name; + } + + + @Override + public boolean isEvergreen() { + return true; + } +} diff --git a/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Tree.java b/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Tree.java new file mode 100644 index 0000000000..0230c61e38 --- /dev/null +++ b/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/Tree.java @@ -0,0 +1,6 @@ +package com.baeldung.object.type; + +public interface Tree { + + public boolean isEvergreen(); +} diff --git a/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/TreeSorter.java b/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/TreeSorter.java new file mode 100644 index 0000000000..af4a6e5ef8 --- /dev/null +++ b/testing-modules/testing-assertions/src/main/java/com/baeldung/object/type/TreeSorter.java @@ -0,0 +1,19 @@ +package com.baeldung.object.type; + +import java.util.List; + +public class TreeSorter { + protected Tree sortTree(String name) { + + final List deciduous = List.of("Beech", "Birch", "Ash", "Whitebeam", "Hornbeam", "Hazel & Willow"); + final List evergreen = List.of("Cedar", "Holly", "Laurel", "Olive", "Pine"); + + if (deciduous.contains(name)) { + return new Deciduous(name); + } else if (evergreen.contains(name)) { + return new Evergreen(name); + } else { + throw new RuntimeException("Tree could not be classified"); + } + } +} diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/object/type/TreeSorterAssertJUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/object/type/TreeSorterAssertJUnitTest.java new file mode 100644 index 0000000000..74b0eec4d9 --- /dev/null +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/object/type/TreeSorterAssertJUnitTest.java @@ -0,0 +1,22 @@ +package com.baeldung.object.type; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class TreeSorterAssertJUnitTest { + private final TreeSorter tested = new TreeSorter(); + + @Test + public void sortTreeShouldReturnEvergreen_WhenPineIsPassed() { + Tree tree = tested.sortTree("Pine"); + assertThat(tree).isExactlyInstanceOf(Evergreen.class); + } + + @Test + public void sortTreeShouldReturnDecidious_WhenBirchIsPassed() { + Tree tree = tested.sortTree("Birch"); + assertThat(tree).hasSameClassAs(new Deciduous("Birch")); + } + +} \ No newline at end of file diff --git a/testing-modules/testing-assertions/src/test/java/com/baeldung/object/type/TreeSorterHamcrestUnitTest.java b/testing-modules/testing-assertions/src/test/java/com/baeldung/object/type/TreeSorterHamcrestUnitTest.java new file mode 100644 index 0000000000..7d35d5c0d0 --- /dev/null +++ b/testing-modules/testing-assertions/src/test/java/com/baeldung/object/type/TreeSorterHamcrestUnitTest.java @@ -0,0 +1,28 @@ +package com.baeldung.object.type; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.instanceOf; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; + +class TreeSorterHamcrestUnitTest { + private final TreeSorter tested = new TreeSorter(); + + @Test + public void sortTreeShouldReturnEvergreen_WhenPineIsPassed() { + Tree tree = tested.sortTree("Pine"); + //with JUnit assertEquals: + assertEquals(tree.getClass(), Evergreen.class); + //with Hamcrest instanceOf: + assertThat(tree, instanceOf(Evergreen.class)); + + } + + @Test + public void sortTreeShouldReturnDecidious_WhenBirchIsPassed() { + Tree tree = tested.sortTree("Birch"); + assertThat(tree, instanceOf(Deciduous.class)); + } + +} \ No newline at end of file From 151753d3ac2b2d4be8aa07cc95a33b8a1420eabf Mon Sep 17 00:00:00 2001 From: Trixi Turny Date: Mon, 8 Nov 2021 17:52:38 +0000 Subject: [PATCH 2/2] BAEL-5209 add to modules sdk9 and above --- pom.xml | 2 ++ testing-modules/pom.xml | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f2a53f38b7..3b396bd2c5 100644 --- a/pom.xml +++ b/pom.xml @@ -1310,6 +1310,7 @@ persistence-modules/sirix quarkus-vs-springboot spring-boot-modules/spring-boot-cassandre + testing-modules/testing-assertions @@ -1362,6 +1363,7 @@ persistence-modules/sirix quarkus-vs-springboot spring-boot-modules/spring-boot-cassandre + testing-modules/testing-assertions diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index 079c7fa792..1e4ff246f9 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -42,7 +42,6 @@ spring-testing-2 spring-testing test-containers - testing-assertions testing-libraries-2 testing-libraries testng