Skip to content

Commit bcc31e1

Browse files
committed
Add JUnit 5 examples
2 parents 4c39dba + e641ca0 commit bcc31e1

6 files changed

Lines changed: 261 additions & 0 deletions

File tree

‎junit5/pom.xml‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?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>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>junit5</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<name>junit5</name>
12+
<description>Intro to JUnit 5</description>
13+
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>
20+
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>
35+
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>
50+
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>
83+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.baeldung;
2+
3+
import org.junit.gen5.api.Test;
4+
5+
import static org.junit.gen5.api.Assertions.assertEquals;
6+
import static org.junit.gen5.api.Assumptions.*;
7+
8+
public class AssumptionTest {
9+
10+
@Test
11+
void trueAssumption() {
12+
assumeTrue(5 > 1);
13+
assertEquals(5 + 2, 7);
14+
}
15+
16+
@Test
17+
void falseAssumption() {
18+
assumeFalse(5 < 1);
19+
assertEquals(5 + 2, 7);
20+
}
21+
22+
@Test
23+
void assumptionThat() {
24+
String someString = "Just a string";
25+
assumingThat(
26+
someString.equals("Just a string"),
27+
() -> assertEquals(2 + 2, 4)
28+
);
29+
}
30+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung;
2+
3+
import org.junit.gen5.api.Test;
4+
5+
import static org.junit.gen5.api.Assertions.assertEquals;
6+
import static org.junit.gen5.api.Assertions.expectThrows;
7+
8+
public class ExceptionTest {
9+
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+
}
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung;
2+
3+
import org.junit.gen5.api.Disabled;
4+
import org.junit.gen5.api.Test;
5+
6+
import java.util.Arrays;
7+
import java.util.List;
8+
9+
import static org.junit.gen5.api.Assertions.*;
10+
11+
class FirstTest {
12+
13+
@Test
14+
void lambdaExpressions() {
15+
List<Integer> numbers = Arrays.asList(1, 2, 3);
16+
assertTrue(numbers
17+
.stream()
18+
.mapToInt(i -> i)
19+
.sum() > 5, "Sum should be greater than 5");
20+
}
21+
22+
@Test
23+
void groupAssertions() {
24+
int[] numbers = {0, 1, 2, 3, 4};
25+
assertAll("numbers",
26+
() -> assertEquals(numbers[0], 1),
27+
() -> assertEquals(numbers[3], 3),
28+
() -> assertEquals(numbers[4], 1)
29+
);
30+
}
31+
32+
@Test
33+
@Disabled
34+
void disabledTest() {
35+
assertTrue(false);
36+
}
37+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung;
2+
3+
import org.junit.gen5.api.*;
4+
5+
import java.util.EmptyStackException;
6+
import java.util.Stack;
7+
8+
public class NestedTest {
9+
Stack<Object> stack;
10+
boolean isRun = false;
11+
12+
@Test
13+
@DisplayName("is instantiated with new Stack()")
14+
void isInstantiatedWithNew() {
15+
new Stack<Object>();
16+
}
17+
18+
@Nested
19+
@DisplayName("when new")
20+
class WhenNew {
21+
22+
@BeforeEach
23+
void init() {
24+
stack = new Stack<Object>();
25+
}
26+
27+
@Test
28+
@DisplayName("is empty")
29+
void isEmpty() {
30+
Assertions.assertTrue(stack.isEmpty());
31+
}
32+
33+
@Test
34+
@DisplayName("throws EmptyStackException when popped")
35+
void throwsExceptionWhenPopped() {
36+
Assertions.expectThrows(EmptyStackException.class, () -> stack.pop());
37+
}
38+
39+
@Test
40+
@DisplayName("throws EmptyStackException when peeked")
41+
void throwsExceptionWhenPeeked() {
42+
Assertions.expectThrows(EmptyStackException.class, () -> stack.peek());
43+
}
44+
45+
@Nested
46+
@DisplayName("after pushing an element")
47+
class AfterPushing {
48+
49+
String anElement = "an element";
50+
51+
@BeforeEach
52+
void init() {
53+
stack.push(anElement);
54+
}
55+
56+
@Test
57+
@DisplayName("it is no longer empty")
58+
void isEmpty() {
59+
Assertions.assertFalse(stack.isEmpty());
60+
}
61+
62+
@Test
63+
@DisplayName("returns the element when popped and is empty")
64+
void returnElementWhenPopped() {
65+
Assertions.assertEquals(anElement, stack.pop());
66+
Assertions.assertTrue(stack.isEmpty());
67+
}
68+
69+
@Test
70+
@DisplayName("returns the element when peeked but remains not empty")
71+
void returnElementWhenPeeked() {
72+
Assertions.assertEquals(anElement, stack.peek());
73+
Assertions.assertFalse(stack.isEmpty());
74+
}
75+
}
76+
}
77+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.baeldung;
2+
3+
import org.junit.gen5.api.Tag;
4+
import org.junit.gen5.api.Test;
5+
6+
import static org.junit.gen5.api.Assertions.assertEquals;
7+
8+
@Tag("Test case")
9+
public class TaggedTest {
10+
11+
@Test
12+
@Tag("Method")
13+
void testMethod() {
14+
assertEquals(2 + 2, 4);
15+
}
16+
}

0 commit comments

Comments
 (0)