├── README.md
├── pom.xml
└── src
└── test
└── java
└── com
└── javadevelopersguide
└── junit5
├── AssertionsDemo.java
├── AssumptionsDemo.java
├── DisabledClassDemo.java
├── DisplayNameDemo.java
├── FirstJUnit5Tests.java
├── Person.java
├── RepeatedTestDemo.java
├── RepeatedTestsDemo.java
├── StandardTests.java
├── TaggedUnitTest.java
├── TestSuite.java
└── TestingAStackDemo.java
/README.md:
--------------------------------------------------------------------------------
1 |
39 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
3 | 4.0.0
4 | com.javadevelopersguide.juni5
5 | junit5-developers-guide
6 | 0.0.1-SNAPSHOT
7 |
8 |
9 |
10 | org.junit.jupiter
11 | junit-jupiter-engine
12 | ${junit.jupiter.version}
13 | test
14 |
15 |
16 |
17 |
18 | 1.2.0
19 | 5.2.0
20 | 1.8
21 |
22 |
23 |
24 |
25 |
26 | maven-compiler-plugin
27 | 3.7.0
28 |
29 | ${java.version}
30 | ${java.version}
31 |
32 |
33 |
34 | maven-surefire-plugin
35 | 2.21.0
36 |
37 |
38 | org.junit.platform
39 | junit-platform-surefire-provider
40 | ${junit.platform.version}
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/AssertionsDemo.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import static java.time.Duration.ofMillis;
4 | import static java.time.Duration.ofMinutes;
5 | import static org.junit.jupiter.api.Assertions.assertAll;
6 | import static org.junit.jupiter.api.Assertions.assertEquals;
7 | import static org.junit.jupiter.api.Assertions.assertNotNull;
8 | import static org.junit.jupiter.api.Assertions.assertThrows;
9 | import static org.junit.jupiter.api.Assertions.assertTimeout;
10 | import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
11 | import static org.junit.jupiter.api.Assertions.assertTrue;
12 |
13 | import org.junit.jupiter.api.Test;
14 |
15 | class AssertionsDemo {
16 |
17 | @Test
18 | void standardAssertions() {
19 | assertEquals(2, 2);
20 | assertEquals(4, 4, "The optional assertion message is now the last parameter.");
21 | assertTrue('a' < 'b', () -> "Assertion messages can be lazily evaluated -- "
22 | + "to avoid constructing complex messages unnecessarily.");
23 | }
24 |
25 | /* @Test
26 | void groupedAssertions() {
27 | // In a grouped assertion all assertions are executed, and any
28 | // failures will be reported together.
29 | assertAll("person",
30 | () -> assertEquals("John", person.getFirstName()),
31 | () -> assertEquals("Doe", person.getFirstName())
32 | );
33 | }*/
34 |
35 | @Test
36 | void dependentAssertions() {
37 | // Within a code block, if an assertion fails the
38 | // subsequent code in the same block will be skipped.
39 | assertAll("properties",
40 | () -> {
41 | String firstName = Person.getFirstName();
42 | assertNotNull(firstName);
43 |
44 | // Executed only if the previous assertion is valid.
45 | assertAll("first name",
46 | () -> assertTrue(firstName.startsWith("J")),
47 | () -> assertTrue(firstName.endsWith("n"))
48 | );
49 | },
50 | () -> {
51 | // Grouped assertion, so processed independently
52 | // of results of first name assertions.
53 | String lastName = "Doe";
54 | assertNotNull(lastName);
55 |
56 | // Executed only if the previous assertion is valid.
57 | assertAll("last name",
58 | () -> assertTrue(lastName.startsWith("D")),
59 | () -> assertTrue(lastName.endsWith("e"))
60 | );
61 | }
62 | );
63 | }
64 |
65 | @Test
66 | void exceptionTesting() {
67 | Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
68 | throw new IllegalArgumentException("a message");
69 | });
70 | assertEquals("a message", exception.getMessage());
71 | }
72 |
73 | @Test
74 | void timeoutNotExceeded() {
75 | // The following assertion succeeds.
76 | assertTimeout(ofMinutes(2), () -> {
77 | // Perform task that takes less than 2 minutes.
78 | });
79 | }
80 |
81 | @Test
82 | void timeoutNotExceededWithResult() {
83 | // The following assertion succeeds, and returns the supplied object.
84 | String actualResult = assertTimeout(ofMinutes(2), () -> {
85 | return "a result";
86 | });
87 | assertEquals("a result", actualResult);
88 | }
89 |
90 | @Test
91 | void timeoutNotExceededWithMethod() {
92 | // The following assertion invokes a method reference and returns an object.
93 | String actualGreeting = assertTimeout(ofMinutes(2), AssertionsDemo::greeting);
94 | assertEquals("Hello, World!", actualGreeting);
95 | }
96 |
97 | @Test
98 | void timeoutExceeded() {
99 | // The following assertion fails with an error message similar to:
100 | // execution exceeded timeout of 10 ms by 91 ms
101 | assertTimeout(ofMillis(10), () -> {
102 | // Simulate task that takes more than 10 ms.
103 | Thread.sleep(100);
104 | });
105 | }
106 |
107 | @Test
108 | void timeoutExceededWithPreemptiveTermination() {
109 | // The following assertion fails with an error message similar to:
110 | // execution timed out after 10 ms
111 | assertTimeoutPreemptively(ofMillis(10), () -> {
112 | // Simulate task that takes more than 10 ms.
113 | Thread.sleep(100);
114 | });
115 | }
116 |
117 | private static String greeting() {
118 | return "Hello, World!";
119 | }
120 |
121 | }
122 |
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/AssumptionsDemo.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 | import static org.junit.jupiter.api.Assumptions.assumeTrue;
5 | import static org.junit.jupiter.api.Assumptions.assumingThat;
6 |
7 | import org.junit.jupiter.api.Test;
8 |
9 | class AssumptionsDemo {
10 |
11 | @Test
12 | void testOnlyOnCiServer() {
13 | assumeTrue("CI".equals(System.getenv("ENV")));
14 | // remainder of test
15 | }
16 |
17 | @Test
18 | void testOnlyOnDeveloperWorkstation() {
19 | assumeTrue("DEV".equals(System.getenv("ENV")),
20 | () -> "Aborting test: not on developer workstation");
21 | // remainder of test
22 | }
23 |
24 | @Test
25 | void testInAllEnvironments() {
26 | assumingThat("CI".equals(System.getenv("ENV")),
27 | () -> {
28 | // perform these assertions only on the CI server
29 | assertEquals(2, 2);
30 | });
31 |
32 | // perform these assertions in all environments
33 | assertEquals("a string", "a string");
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/DisabledClassDemo.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import org.junit.jupiter.api.Disabled;
4 | import org.junit.jupiter.api.Test;
5 |
6 | @Disabled
7 | class DisabledClassDemo {
8 | @Test
9 | void testWillBeSkipped() {
10 | System.out.println("testWillBeSkipped");
11 | }
12 |
13 | @Test
14 | void testWillBeExecuted() {
15 | System.out.println("testWillBeExecuted");
16 | }
17 |
18 | @Test
19 | void test1() {
20 | System.out.println("test 1");
21 | }
22 |
23 | @Test
24 | void test2() {
25 | System.out.println("test 2");
26 | }
27 |
28 | @Test
29 | void test3() {
30 | System.out.println("test 2");
31 | }
32 | }
33 |
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/DisplayNameDemo.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import org.junit.jupiter.api.DisplayName;
4 | import org.junit.jupiter.api.Test;
5 |
6 | @DisplayName("A special test case")
7 | class DisplayNameDemo {
8 |
9 | @Test
10 | @DisplayName("Custom test name containing spaces")
11 | void testWithDisplayNameContainingSpaces() {
12 | }
13 |
14 | @Test
15 | @DisplayName("╯°□°)╯")
16 | void testWithDisplayNameContainingSpecialCharacters() {
17 | }
18 |
19 | @Test
20 | @DisplayName("😱")
21 | void testWithDisplayNameContainingEmoji() {
22 | }
23 |
24 | }
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/FirstJUnit5Tests.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 |
5 | import org.junit.jupiter.api.Test;
6 |
7 | class FirstJUnit5Tests {
8 |
9 | @Test
10 | void myFirstTest() {
11 | assertEquals(2, 1 + 1);
12 | }
13 |
14 | }
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/Person.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | public class Person {
4 |
5 | public static String getFirstName() {
6 | // TODO Auto-generated method stub
7 | return "John";
8 | }
9 |
10 | }
11 |
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/RepeatedTestDemo.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 |
5 | import org.junit.jupiter.api.AfterEach;
6 | import org.junit.jupiter.api.BeforeEach;
7 | import org.junit.jupiter.api.RepeatedTest;
8 | import org.junit.jupiter.api.RepetitionInfo;
9 | import org.junit.jupiter.api.TestInfo;
10 |
11 | public class RepeatedTestDemo {
12 |
13 | @BeforeEach
14 | void beforeEachTest() {
15 | System.out.println("Before Each Test");
16 | }
17 |
18 | @AfterEach
19 | void afterEachTest() {
20 | System.out.println("After Each Test");
21 | System.out.println("=====================");
22 | }
23 |
24 | @RepeatedTest(3)
25 | void repeatedTest(TestInfo testInfo) {
26 | System.out.println("Executing repeated test");
27 | assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
28 | }
29 |
30 | @RepeatedTest(value = 3, name = RepeatedTest.LONG_DISPLAY_NAME)
31 | void repeatedTestWithLongName() {
32 | System.out.println("Executing repeated test with long name");
33 | assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
34 | }
35 |
36 | @RepeatedTest(value = 3, name = RepeatedTest.SHORT_DISPLAY_NAME)
37 | void repeatedTestWithShortName() {
38 | System.out.println("Executing repeated test with long name");
39 | assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
40 | }
41 |
42 | @RepeatedTest(value = 3, name = "Custom name {currentRepetition}/{totalRepetitions}")
43 | void repeatedTestWithCustomDisplayName() {
44 | assertEquals(2, Math.addExact(1, 1), "1 + 1 should equal 2");
45 | }
46 |
47 | @RepeatedTest(3)
48 | void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
49 | System.out.println("Repetition #" + repetitionInfo.getCurrentRepetition());
50 | assertEquals(3, repetitionInfo.getTotalRepetitions());
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/RepeatedTestsDemo.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 |
5 | import org.junit.jupiter.api.BeforeEach;
6 | import org.junit.jupiter.api.DisplayName;
7 | import org.junit.jupiter.api.RepeatedTest;
8 | import org.junit.jupiter.api.RepetitionInfo;
9 | import org.junit.jupiter.api.TestInfo;
10 |
11 | class RepeatedTestsDemo {
12 |
13 |
14 | @BeforeEach
15 | void beforeEach(TestInfo testInfo, RepetitionInfo repetitionInfo) {
16 | int currentRepetition = repetitionInfo.getCurrentRepetition();
17 | int totalRepetitions = repetitionInfo.getTotalRepetitions();
18 | String methodName = testInfo.getTestMethod().get().getName();
19 | }
20 |
21 | @RepeatedTest(10)
22 | void repeatedTest() {
23 | // ...
24 | }
25 |
26 | @RepeatedTest(5)
27 | void repeatedTestWithRepetitionInfo(RepetitionInfo repetitionInfo) {
28 | assertEquals(5, repetitionInfo.getTotalRepetitions());
29 | }
30 |
31 | @RepeatedTest(value = 1, name = "{displayName} {currentRepetition}/{totalRepetitions}")
32 | @DisplayName("Repeat!")
33 | void customDisplayName(TestInfo testInfo) {
34 | assertEquals(testInfo.getDisplayName(), "Repeat! 1/1");
35 | }
36 |
37 | @RepeatedTest(value = 1, name = RepeatedTest.LONG_DISPLAY_NAME)
38 | @DisplayName("Details...")
39 | void customDisplayNameWithLongPattern(TestInfo testInfo) {
40 | assertEquals(testInfo.getDisplayName(), "Details... :: repetition 1 of 1");
41 | }
42 |
43 | @RepeatedTest(value = 5, name = "Wiederholung {currentRepetition} von {totalRepetitions}")
44 | void repeatedTestInGerman() {
45 | // ...
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/StandardTests.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import static org.junit.jupiter.api.Assertions.fail;
4 |
5 | import org.junit.jupiter.api.AfterAll;
6 | import org.junit.jupiter.api.AfterEach;
7 | import org.junit.jupiter.api.BeforeAll;
8 | import org.junit.jupiter.api.BeforeEach;
9 | import org.junit.jupiter.api.Disabled;
10 | import org.junit.jupiter.api.Test;
11 |
12 | class StandardTests {
13 |
14 | @BeforeAll
15 | static void initAll() {
16 | }
17 |
18 | @BeforeEach
19 | void init() {
20 | }
21 |
22 | @Test
23 | void succeedingTest() {
24 | }
25 |
26 |
27 | // This will fail the test case
28 | // uncomment fail method to run
29 | @Test
30 | void failingTest() {
31 | //fail("a failing test");
32 | }
33 |
34 | @Test
35 | @Disabled("for demonstration purposes")
36 | void skippedTest() {
37 | // not executed
38 | }
39 |
40 | @AfterEach
41 | void tearDown() {
42 | }
43 |
44 | @AfterAll
45 | static void tearDownAll() {
46 | }
47 |
48 | }
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/TaggedUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 |
5 | import org.junit.jupiter.api.Tag;
6 | import org.junit.jupiter.api.Test;
7 |
8 | @Tag("Test case")
9 | public class TaggedUnitTest {
10 |
11 | @Test
12 | @Tag("Method")
13 | void testMethod() {
14 | assertEquals(2 + 2, 4);
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/TestSuite.java:
--------------------------------------------------------------------------------
1 | /*package com.javadevelopersguide.junit5;
2 |
3 | import org.junit.platform.runner.JUnitPlatform;
4 | import org.junit.platform.suite.api.SelectClasses;
5 | import org.junit.platform.suite.api.SelectPackages;
6 | import org.junit.runner.RunWith;
7 |
8 | @RunWith(JUnitPlatform.class)
9 | //@SelectPackages("com.javadevelopersguide.junit5")
10 | @SelectClasses({AssertionsDemo.class, AssumptionsDemo.class, DisabledClassDemo.class})
11 | public class TestSuite {
12 |
13 | }
14 | */
--------------------------------------------------------------------------------
/src/test/java/com/javadevelopersguide/junit5/TestingAStackDemo.java:
--------------------------------------------------------------------------------
1 | package com.javadevelopersguide.junit5;
2 |
3 | import static org.junit.jupiter.api.Assertions.assertEquals;
4 | import static org.junit.jupiter.api.Assertions.assertFalse;
5 | import static org.junit.jupiter.api.Assertions.assertThrows;
6 | import static org.junit.jupiter.api.Assertions.assertTrue;
7 |
8 | import java.util.EmptyStackException;
9 | import java.util.Stack;
10 |
11 | import org.junit.jupiter.api.BeforeEach;
12 | import org.junit.jupiter.api.DisplayName;
13 | import org.junit.jupiter.api.Nested;
14 | import org.junit.jupiter.api.Test;
15 |
16 | @DisplayName("A stack")
17 | class TestingAStackDemo {
18 |
19 | Stack