From c574b94ea34092088373bd96d1ce42a8582b751a Mon Sep 17 00:00:00 2001 From: amedviediev Date: Fri, 15 Apr 2016 23:47:32 +0300 Subject: [PATCH 1/5] - Added code for JUnit 5 article --- junit5/pom.xml | 83 +++++++++++++++++++ .../java/com/baeldung/AssumptionTest.java | 31 +++++++ .../test/java/com/baeldung/ExceptionTest.java | 15 ++++ .../src/test/java/com/baeldung/FirstTest.java | 27 ++++++ .../test/java/com/baeldung/NestedTest.java | 77 +++++++++++++++++ 5 files changed, 233 insertions(+) create mode 100644 junit5/pom.xml create mode 100644 junit5/src/test/java/com/baeldung/AssumptionTest.java create mode 100644 junit5/src/test/java/com/baeldung/ExceptionTest.java create mode 100644 junit5/src/test/java/com/baeldung/FirstTest.java create mode 100644 junit5/src/test/java/com/baeldung/NestedTest.java diff --git a/junit5/pom.xml b/junit5/pom.xml new file mode 100644 index 0000000000..5a2ea61668 --- /dev/null +++ b/junit5/pom.xml @@ -0,0 +1,83 @@ + + + 4.0.0 + + com.baeldung + junit5 + 1.0-SNAPSHOT + + junit5 + Intro to JUnit 5 + + + UTF-8 + 1.8 + + 5.0.0-SNAPSHOT + + + + + snapshots-repo + https://oss.sonatype.org/content/repositories/snapshots + + false + + + + always + true + + + + + + + snapshots-repo + https://oss.sonatype.org/content/repositories/snapshots + + false + + + + always + true + + + + + + + + maven-compiler-plugin + 3.1 + + ${java.version} + ${java.version} + + + + maven-surefire-plugin + 2.19 + + + org.junit + surefire-junit5 + ${junit.gen5.version} + + + + + + + + + org.junit + junit5-api + ${junit.gen5.version} + test + + + \ No newline at end of file diff --git a/junit5/src/test/java/com/baeldung/AssumptionTest.java b/junit5/src/test/java/com/baeldung/AssumptionTest.java new file mode 100644 index 0000000000..6e6e56fe0a --- /dev/null +++ b/junit5/src/test/java/com/baeldung/AssumptionTest.java @@ -0,0 +1,31 @@ +package com.baeldung; + +import org.junit.gen5.api.Test; + +import static org.junit.gen5.api.Assumptions.assumeFalse; +import static org.junit.gen5.api.Assumptions.assumeTrue; +import static org.junit.gen5.api.Assumptions.assumingThat; + +public class AssumptionTest { + + @Test + void trueAssumption() { + assumeTrue(5 > 1); + System.out.println("Was true"); + } + + @Test + void falseAssumption() { + assumeFalse(5 < 1); + System.out.println("Was false"); + } + + @Test + void assumptionThat() { + String someString = "Just a string"; + assumingThat( + someString.equals("Just a string"), + () -> System.out.println("Assumption was correct") + ); + } +} diff --git a/junit5/src/test/java/com/baeldung/ExceptionTest.java b/junit5/src/test/java/com/baeldung/ExceptionTest.java new file mode 100644 index 0000000000..a9a5b8301d --- /dev/null +++ b/junit5/src/test/java/com/baeldung/ExceptionTest.java @@ -0,0 +1,15 @@ +package com.baeldung; + +import org.junit.gen5.api.Test; + +import static org.junit.gen5.api.Assertions.assertEquals; +import static org.junit.gen5.api.Assertions.expectThrows; + +public class ExceptionTest { + + @Test + void shouldThrowException() { + Throwable exception = expectThrows(NullPointerException.class, null); + assertEquals(exception.getClass(), NullPointerException.class); + } +} diff --git a/junit5/src/test/java/com/baeldung/FirstTest.java b/junit5/src/test/java/com/baeldung/FirstTest.java new file mode 100644 index 0000000000..c0dc6f345c --- /dev/null +++ b/junit5/src/test/java/com/baeldung/FirstTest.java @@ -0,0 +1,27 @@ +package com.baeldung; + +import org.junit.gen5.api.Test; + +import static org.junit.gen5.api.Assertions.assertAll; +import static org.junit.gen5.api.Assertions.assertEquals; +import static org.junit.gen5.api.Assertions.assertTrue; + +class FirstTest { + + @Test + void lambdaExpressions() { + String string = ""; + assertTrue(() -> string.isEmpty(), "String should be empty"); + } + + @Test + void groupAssertions() { + int[] numbers = {0,1,2,3,4}; + assertAll("numbers", () -> { + assertEquals(numbers[0], 1); + assertEquals(numbers[3], 3); + assertEquals(numbers[4], 1); + }); + } + +} diff --git a/junit5/src/test/java/com/baeldung/NestedTest.java b/junit5/src/test/java/com/baeldung/NestedTest.java new file mode 100644 index 0000000000..3fbe4f8644 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/NestedTest.java @@ -0,0 +1,77 @@ +package com.baeldung; + +import org.junit.gen5.api.*; + +import java.util.EmptyStackException; +import java.util.Stack; + +public class NestedTest { + Stack stack; + boolean isRun = false; + + @Test + @DisplayName("is instantiated with new Stack()") + void isInstantiatedWithNew() { + new Stack(); + } + + @Nested + @DisplayName("when new") + class WhenNew { + + @BeforeEach + void init() { + stack = new Stack(); + } + + @Test + @DisplayName("is empty") + void isEmpty() { + Assertions.assertTrue(stack.isEmpty()); + } + + @Test + @DisplayName("throws EmptyStackException when popped") + void throwsExceptionWhenPopped() { + Assertions.expectThrows(EmptyStackException.class, () -> stack.pop()); + } + + @Test + @DisplayName("throws EmptyStackException when peeked") + void throwsExceptionWhenPeeked() { + Assertions.expectThrows(EmptyStackException.class, () -> stack.peek()); + } + + @Nested + @DisplayName("after pushing an element") + class AfterPushing { + + String anElement = "an element"; + + @BeforeEach + void init() { + stack.push(anElement); + } + + @Test + @DisplayName("it is no longer empty") + void isEmpty() { + Assertions.assertFalse(stack.isEmpty()); + } + + @Test + @DisplayName("returns the element when popped and is empty") + void returnElementWhenPopped() { + Assertions.assertEquals(anElement, stack.pop()); + Assertions.assertTrue(stack.isEmpty()); + } + + @Test + @DisplayName("returns the element when peeked but remains not empty") + void returnElementWhenPeeked() { + Assertions.assertEquals(anElement, stack.peek()); + Assertions.assertFalse(stack.isEmpty()); + } + } + } +} From a875e25b18f85c80571c27e145fd2816400f04cc Mon Sep 17 00:00:00 2001 From: amedviediev Date: Mon, 2 May 2016 13:00:21 +0300 Subject: [PATCH 2/5] - Added code for JUnit 5 article --- .../test/java/com/baeldung/AssumptionTest.java | 7 ++++--- .../test/java/com/baeldung/ExceptionTest.java | 5 ++++- junit5/src/test/java/com/baeldung/FirstTest.java | 8 +++++++- .../src/test/java/com/baeldung/TaggedTest.java | 16 ++++++++++++++++ 4 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 junit5/src/test/java/com/baeldung/TaggedTest.java diff --git a/junit5/src/test/java/com/baeldung/AssumptionTest.java b/junit5/src/test/java/com/baeldung/AssumptionTest.java index 6e6e56fe0a..634ec9b21e 100644 --- a/junit5/src/test/java/com/baeldung/AssumptionTest.java +++ b/junit5/src/test/java/com/baeldung/AssumptionTest.java @@ -2,6 +2,7 @@ package com.baeldung; import org.junit.gen5.api.Test; +import static org.junit.gen5.api.Assertions.assertEquals; import static org.junit.gen5.api.Assumptions.assumeFalse; import static org.junit.gen5.api.Assumptions.assumeTrue; import static org.junit.gen5.api.Assumptions.assumingThat; @@ -11,13 +12,13 @@ public class AssumptionTest { @Test void trueAssumption() { assumeTrue(5 > 1); - System.out.println("Was true"); + assertEquals(5 + 2, 7); } @Test void falseAssumption() { assumeFalse(5 < 1); - System.out.println("Was false"); + assertEquals(5 + 2, 7); } @Test @@ -25,7 +26,7 @@ public class AssumptionTest { String someString = "Just a string"; assumingThat( someString.equals("Just a string"), - () -> System.out.println("Assumption was correct") + () -> assertEquals(2 + 2, 4) ); } } diff --git a/junit5/src/test/java/com/baeldung/ExceptionTest.java b/junit5/src/test/java/com/baeldung/ExceptionTest.java index a9a5b8301d..26c44e8b10 100644 --- a/junit5/src/test/java/com/baeldung/ExceptionTest.java +++ b/junit5/src/test/java/com/baeldung/ExceptionTest.java @@ -2,6 +2,8 @@ package com.baeldung; import org.junit.gen5.api.Test; +import java.util.ArrayList; + import static org.junit.gen5.api.Assertions.assertEquals; import static org.junit.gen5.api.Assertions.expectThrows; @@ -9,7 +11,8 @@ public class ExceptionTest { @Test void shouldThrowException() { - Throwable exception = expectThrows(NullPointerException.class, null); + ArrayList list = null; + Throwable exception = expectThrows(NullPointerException.class, list::clear); assertEquals(exception.getClass(), NullPointerException.class); } } diff --git a/junit5/src/test/java/com/baeldung/FirstTest.java b/junit5/src/test/java/com/baeldung/FirstTest.java index c0dc6f345c..d7c6c0368f 100644 --- a/junit5/src/test/java/com/baeldung/FirstTest.java +++ b/junit5/src/test/java/com/baeldung/FirstTest.java @@ -1,5 +1,6 @@ package com.baeldung; +import org.junit.gen5.api.Disabled; import org.junit.gen5.api.Test; import static org.junit.gen5.api.Assertions.assertAll; @@ -11,7 +12,7 @@ class FirstTest { @Test void lambdaExpressions() { String string = ""; - assertTrue(() -> string.isEmpty(), "String should be empty"); + assertTrue(string::isEmpty, "String should be empty"); } @Test @@ -24,4 +25,9 @@ class FirstTest { }); } + @Test + @Disabled + void disabledTest() { + assertTrue(false); + } } diff --git a/junit5/src/test/java/com/baeldung/TaggedTest.java b/junit5/src/test/java/com/baeldung/TaggedTest.java new file mode 100644 index 0000000000..5107c04037 --- /dev/null +++ b/junit5/src/test/java/com/baeldung/TaggedTest.java @@ -0,0 +1,16 @@ +package com.baeldung; + +import org.junit.gen5.api.Tag; +import org.junit.gen5.api.Test; + +import static org.junit.gen5.api.Assertions.assertEquals; + +@Tag("Test case") +public class TaggedTest { + + @Test + @Tag("Method") + void testMethod() { + assertEquals(2+2, 4); + } +} From f7176e6255e9cf3b72479e1b48d142bcf8e237f5 Mon Sep 17 00:00:00 2001 From: amedviediev Date: Wed, 11 May 2016 00:00:29 +0300 Subject: [PATCH 3/5] - Added code for JUnit 5 article --- .../src/test/java/com/baeldung/ExceptionTest.java | 6 +++--- junit5/src/test/java/com/baeldung/FirstTest.java | 13 ++++++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/junit5/src/test/java/com/baeldung/ExceptionTest.java b/junit5/src/test/java/com/baeldung/ExceptionTest.java index 26c44e8b10..25b7f0a1cc 100644 --- a/junit5/src/test/java/com/baeldung/ExceptionTest.java +++ b/junit5/src/test/java/com/baeldung/ExceptionTest.java @@ -11,8 +11,8 @@ public class ExceptionTest { @Test void shouldThrowException() { - ArrayList list = null; - Throwable exception = expectThrows(NullPointerException.class, list::clear); - assertEquals(exception.getClass(), NullPointerException.class); + Throwable exception = expectThrows(UnsupportedOperationException.class, + () -> {throw new UnsupportedOperationException("Not supported");}); + assertEquals(exception.getMessage(), "Not supported"); } } diff --git a/junit5/src/test/java/com/baeldung/FirstTest.java b/junit5/src/test/java/com/baeldung/FirstTest.java index d7c6c0368f..aa7bcfdcd0 100644 --- a/junit5/src/test/java/com/baeldung/FirstTest.java +++ b/junit5/src/test/java/com/baeldung/FirstTest.java @@ -3,6 +3,10 @@ package com.baeldung; import org.junit.gen5.api.Disabled; import org.junit.gen5.api.Test; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import static org.junit.gen5.api.Assertions.assertAll; import static org.junit.gen5.api.Assertions.assertEquals; import static org.junit.gen5.api.Assertions.assertTrue; @@ -11,13 +15,16 @@ class FirstTest { @Test void lambdaExpressions() { - String string = ""; - assertTrue(string::isEmpty, "String should be empty"); + List numbers = Arrays.asList(1, 2, 3); + assertTrue(numbers + .stream() + .mapToInt(i -> i) + .sum() > 5, "Sum should be greater than 5"); } @Test void groupAssertions() { - int[] numbers = {0,1,2,3,4}; + int[] numbers = {0, 1, 2, 3, 4}; assertAll("numbers", () -> { assertEquals(numbers[0], 1); assertEquals(numbers[3], 3); From c4fdeee352dfa8fd9ed09c34d4c1491b39340847 Mon Sep 17 00:00:00 2001 From: amedviediev Date: Sat, 21 May 2016 16:25:09 +0300 Subject: [PATCH 4/5] - Fixed assertAll code --- junit5/src/test/java/com/baeldung/FirstTest.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/junit5/src/test/java/com/baeldung/FirstTest.java b/junit5/src/test/java/com/baeldung/FirstTest.java index aa7bcfdcd0..2fa0f31a38 100644 --- a/junit5/src/test/java/com/baeldung/FirstTest.java +++ b/junit5/src/test/java/com/baeldung/FirstTest.java @@ -25,11 +25,11 @@ class FirstTest { @Test void groupAssertions() { int[] numbers = {0, 1, 2, 3, 4}; - assertAll("numbers", () -> { - assertEquals(numbers[0], 1); - assertEquals(numbers[3], 3); - assertEquals(numbers[4], 1); - }); + assertAll("numbers", + () -> assertEquals(numbers[0], 1), + () -> assertEquals(numbers[3], 3), + () -> assertEquals(numbers[4], 1) + ); } @Test From e641ca0598564fea42547903f8108006ba7244ae Mon Sep 17 00:00:00 2001 From: David Morley Date: Mon, 23 May 2016 06:14:18 -0500 Subject: [PATCH 5/5] Reformat JUnit 5 examples --- junit5/src/test/java/com/baeldung/AssumptionTest.java | 4 +--- junit5/src/test/java/com/baeldung/ExceptionTest.java | 6 +++--- junit5/src/test/java/com/baeldung/FirstTest.java | 5 +---- junit5/src/test/java/com/baeldung/TaggedTest.java | 2 +- 4 files changed, 6 insertions(+), 11 deletions(-) diff --git a/junit5/src/test/java/com/baeldung/AssumptionTest.java b/junit5/src/test/java/com/baeldung/AssumptionTest.java index 634ec9b21e..e4c2b56124 100644 --- a/junit5/src/test/java/com/baeldung/AssumptionTest.java +++ b/junit5/src/test/java/com/baeldung/AssumptionTest.java @@ -3,9 +3,7 @@ package com.baeldung; import org.junit.gen5.api.Test; import static org.junit.gen5.api.Assertions.assertEquals; -import static org.junit.gen5.api.Assumptions.assumeFalse; -import static org.junit.gen5.api.Assumptions.assumeTrue; -import static org.junit.gen5.api.Assumptions.assumingThat; +import static org.junit.gen5.api.Assumptions.*; public class AssumptionTest { diff --git a/junit5/src/test/java/com/baeldung/ExceptionTest.java b/junit5/src/test/java/com/baeldung/ExceptionTest.java index 25b7f0a1cc..5c30ad5b44 100644 --- a/junit5/src/test/java/com/baeldung/ExceptionTest.java +++ b/junit5/src/test/java/com/baeldung/ExceptionTest.java @@ -2,8 +2,6 @@ package com.baeldung; import org.junit.gen5.api.Test; -import java.util.ArrayList; - import static org.junit.gen5.api.Assertions.assertEquals; import static org.junit.gen5.api.Assertions.expectThrows; @@ -12,7 +10,9 @@ public class ExceptionTest { @Test void shouldThrowException() { Throwable exception = expectThrows(UnsupportedOperationException.class, - () -> {throw new UnsupportedOperationException("Not supported");}); + () -> { + throw new UnsupportedOperationException("Not supported"); + }); assertEquals(exception.getMessage(), "Not supported"); } } diff --git a/junit5/src/test/java/com/baeldung/FirstTest.java b/junit5/src/test/java/com/baeldung/FirstTest.java index 2fa0f31a38..4fc6037174 100644 --- a/junit5/src/test/java/com/baeldung/FirstTest.java +++ b/junit5/src/test/java/com/baeldung/FirstTest.java @@ -3,13 +3,10 @@ package com.baeldung; import org.junit.gen5.api.Disabled; import org.junit.gen5.api.Test; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static org.junit.gen5.api.Assertions.assertAll; -import static org.junit.gen5.api.Assertions.assertEquals; -import static org.junit.gen5.api.Assertions.assertTrue; +import static org.junit.gen5.api.Assertions.*; class FirstTest { diff --git a/junit5/src/test/java/com/baeldung/TaggedTest.java b/junit5/src/test/java/com/baeldung/TaggedTest.java index 5107c04037..dbc82f4022 100644 --- a/junit5/src/test/java/com/baeldung/TaggedTest.java +++ b/junit5/src/test/java/com/baeldung/TaggedTest.java @@ -11,6 +11,6 @@ public class TaggedTest { @Test @Tag("Method") void testMethod() { - assertEquals(2+2, 4); + assertEquals(2 + 2, 4); } }