Skip to content

Commit 9b0aa76

Browse files
committed
Junit 5
1 parent 94bfcd3 commit 9b0aa76

12 files changed

Lines changed: 217 additions & 97 deletions
File renamed without changes.

‎junit5/pom.xml‎

Lines changed: 45 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,53 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<project xmlns="http://maven.apache.org/POM/4.0.0"
3-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5-
<modelVersion>4.0.0</modelVersion>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
65

7-
<groupId>com.baeldung</groupId>
8-
<artifactId>junit5</artifactId>
9-
<version>1.0-SNAPSHOT</version>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>junit5</artifactId>
8+
<version>1.0-SNAPSHOT</version>
109

11-
<name>junit5</name>
12-
<description>Intro to JUnit 5</description>
10+
<name>junit5</name>
11+
<description>Intro to JUnit 5</description>
1312

14-
<properties>
15-
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16-
<java.version>1.8</java.version>
17-
<!-- Due to a bug in surefire-junit5-5.0.0-ALPHA we use the latest snapshot instead -->
18-
<junit.gen5.version>5.0.0-SNAPSHOT</junit.gen5.version>
19-
</properties>
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<java.version>1.8</java.version>
16+
<junit.jupiter.version>5.0.0-M2</junit.jupiter.version>
17+
<junit.platform.version>1.0.0-M2</junit.platform.version>
18+
</properties>
2019

21-
<repositories>
22-
<repository>
23-
<id>snapshots-repo</id>
24-
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
25-
<releases>
26-
<enabled>false</enabled>
27-
</releases>
28-
<snapshots>
29-
<!-- Do NOT cache JUnit 5 snapshot JARs. -->
30-
<updatePolicy>always</updatePolicy>
31-
<enabled>true</enabled>
32-
</snapshots>
33-
</repository>
34-
</repositories>
20+
<build>
21+
<plugins>
22+
<plugin>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.1</version>
25+
<configuration>
26+
<source>${java.version}</source>
27+
<target>${java.version}</target>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<artifactId>maven-surefire-plugin</artifactId>
32+
<version>2.19</version>
33+
<dependencies>
34+
<dependency>
35+
<groupId>org.junit.platform</groupId>
36+
<artifactId>junit-platform-surefire-provider</artifactId>
37+
<version>${junit.platform.version}</version>
38+
</dependency>
39+
</dependencies>
40+
</plugin>
41+
</plugins>
42+
</build>
3543

36-
<pluginRepositories>
37-
<pluginRepository>
38-
<id>snapshots-repo</id>
39-
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
40-
<releases>
41-
<enabled>false</enabled>
42-
</releases>
43-
<snapshots>
44-
<!-- Do NOT cache JUnit 5 snapshot JARs. -->
45-
<updatePolicy>always</updatePolicy>
46-
<enabled>true</enabled>
47-
</snapshots>
48-
</pluginRepository>
49-
</pluginRepositories>
44+
<dependencies>
45+
<dependency>
46+
<groupId>org.junit.jupiter</groupId>
47+
<artifactId>junit-jupiter-engine</artifactId>
48+
<version>${junit.jupiter.version}</version>
49+
<scope>test</scope>
50+
</dependency>
5051

51-
<build>
52-
<plugins>
53-
<plugin>
54-
<artifactId>maven-compiler-plugin</artifactId>
55-
<version>3.1</version>
56-
<configuration>
57-
<source>${java.version}</source>
58-
<target>${java.version}</target>
59-
</configuration>
60-
</plugin>
61-
<plugin>
62-
<artifactId>maven-surefire-plugin</artifactId>
63-
<version>2.19</version>
64-
<dependencies>
65-
<dependency>
66-
<groupId>org.junit</groupId>
67-
<artifactId>surefire-junit5</artifactId>
68-
<version>${junit.gen5.version}</version>
69-
</dependency>
70-
</dependencies>
71-
</plugin>
72-
</plugins>
73-
</build>
74-
75-
<dependencies>
76-
<dependency>
77-
<groupId>org.junit</groupId>
78-
<artifactId>junit5-api</artifactId>
79-
<version>${junit.gen5.version}</version>
80-
<scope>test</scope>
81-
</dependency>
82-
</dependencies>
52+
</dependencies>
8353
</project>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung;
2+
3+
import static org.junit.jupiter.api.Assertions.assertThrows;
4+
import static org.junit.jupiter.api.Assertions.expectThrows;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
public class AssertionTest {
9+
10+
@Test
11+
public void testConvertToDoubleThrowException() {
12+
String age = "eighteen";
13+
expectThrows(NumberFormatException.class, () -> {
14+
convertToInt(age);
15+
});
16+
17+
assertThrows(NumberFormatException.class, () -> {
18+
convertToInt(age);
19+
});
20+
}
21+
22+
private static Integer convertToInt(String str) {
23+
if (str == null) {
24+
return null;
25+
}
26+
return Integer.valueOf(str);
27+
}
28+
29+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.baeldung;
22

3-
import org.junit.gen5.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assumptions.assumeFalse;
5+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
6+
import static org.junit.jupiter.api.Assumptions.assumingThat;
47

5-
import static org.junit.gen5.api.Assertions.assertEquals;
6-
import static org.junit.gen5.api.Assumptions.*;
8+
import org.junit.jupiter.api.Test;
79

810
public class AssumptionTest {
911

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package com.baeldung;
22

3-
import org.junit.gen5.api.Test;
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.expectThrows;
46

5-
import static org.junit.gen5.api.Assertions.assertEquals;
6-
import static org.junit.gen5.api.Assertions.expectThrows;
7+
import org.junit.jupiter.api.Test;
78

89
public class ExceptionTest {
910

10-
@Test
11-
void shouldThrowException() {
12-
Throwable exception = expectThrows(UnsupportedOperationException.class,
13-
() -> {
14-
throw new UnsupportedOperationException("Not supported");
15-
});
16-
assertEquals(exception.getMessage(), "Not supported");
17-
}
11+
@Test
12+
void shouldThrowException() {
13+
Throwable exception = expectThrows(UnsupportedOperationException.class, () -> {
14+
throw new UnsupportedOperationException("Not supported");
15+
});
16+
assertEquals(exception.getMessage(), "Not supported");
17+
}
18+
19+
@Test
20+
public void convertToIntNullParameterAssertThrows() {
21+
String str = null;
22+
assertThrows(IllegalArgumentException.class, () -> {
23+
Integer.valueOf(str);
24+
});
25+
}
1826
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.baeldung;
22

3-
import org.junit.gen5.api.Disabled;
4-
import org.junit.gen5.api.Test;
3+
import static org.junit.jupiter.api.Assertions.*;
54

65
import java.util.Arrays;
76
import java.util.List;
87

9-
import static org.junit.gen5.api.Assertions.*;
8+
import org.junit.jupiter.api.Disabled;
9+
import org.junit.jupiter.api.Test;
1010

1111
class FirstTest {
1212

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung;
2+
3+
import java.util.logging.Logger;
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.DisplayName;
11+
import org.junit.jupiter.api.Test;
12+
13+
//@RunWith(JUnitPlatform.class)
14+
public class JUnit5NewFeaturesTest {
15+
16+
private static final Logger log = Logger.getLogger(JUnit5NewFeaturesTest.class.getName());
17+
18+
@BeforeAll
19+
static void setup() {
20+
log.info("@BeforeAll - executes once before all test methods in this class");
21+
}
22+
23+
@BeforeEach
24+
void init() {
25+
log.info("@BeforeEach - executes before each test method in this class");
26+
}
27+
28+
@DisplayName("Single test successful")
29+
@Test
30+
void testSingleSuccessTest() {
31+
log.info("Success");
32+
33+
}
34+
35+
@Test
36+
@Disabled("Not implemented yet.")
37+
void testShowSomething() {
38+
}
39+
40+
@AfterEach
41+
void tearDown() {
42+
log.info("@AfterEach - executed after each test method.");
43+
}
44+
45+
@AfterAll
46+
static void done() {
47+
log.info("@AfterAll - executed after all test methods.");
48+
}
49+
50+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.List;
8+
import java.util.stream.Stream;
9+
10+
import org.junit.jupiter.api.DynamicTest;
11+
import org.junit.jupiter.api.TestFactory;
12+
13+
public class LiveTest {
14+
15+
private List<String> in = new ArrayList<>(Arrays.asList("Hello", "Yes", "No"));
16+
private List<String> out = new ArrayList<>(Arrays.asList("Cześć", "Tak", "Nie"));
17+
18+
@TestFactory
19+
public Stream<DynamicTest> translateDynamicTestsFromStream() {
20+
21+
return in.stream().map(word -> DynamicTest.dynamicTest("Test translate " + word, () -> {
22+
int id = in.indexOf(word);
23+
assertEquals(out.get(id), translate(word));
24+
}));
25+
}
26+
27+
private String translate(String word) {
28+
if ("Hello".equalsIgnoreCase(word)) {
29+
return "Cześć";
30+
} else if ("Yes".equalsIgnoreCase(word)) {
31+
return "Tak";
32+
} else if ("No".equalsIgnoreCase(word)) {
33+
return "Nie";
34+
}
35+
return "Error";
36+
}
37+
38+
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.baeldung;
22

3-
import org.junit.gen5.api.*;
4-
53
import java.util.EmptyStackException;
64
import java.util.Stack;
75

6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.junit.jupiter.api.DisplayName;
9+
import org.junit.jupiter.api.Nested;
10+
import org.junit.jupiter.api.Test;
11+
812
public class NestedTest {
913
Stack<Object> stack;
1014
boolean isRun = false;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung;
2+
3+
public final class StringUtils {
4+
5+
public static Double convertToDouble(String str) {
6+
if (str == null) {
7+
return null;
8+
}
9+
return Double.valueOf(str);
10+
}
11+
}

0 commit comments

Comments
 (0)