Skip to content

Commit c574b94

Browse files
committed
- Added code for JUnit 5 article
1 parent 2ce4f5a commit c574b94

5 files changed

Lines changed: 233 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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.baeldung;
2+
3+
import org.junit.gen5.api.Test;
4+
5+
import static org.junit.gen5.api.Assumptions.assumeFalse;
6+
import static org.junit.gen5.api.Assumptions.assumeTrue;
7+
import static org.junit.gen5.api.Assumptions.assumingThat;
8+
9+
public class AssumptionTest {
10+
11+
@Test
12+
void trueAssumption() {
13+
assumeTrue(5 > 1);
14+
System.out.println("Was true");
15+
}
16+
17+
@Test
18+
void falseAssumption() {
19+
assumeFalse(5 < 1);
20+
System.out.println("Was false");
21+
}
22+
23+
@Test
24+
void assumptionThat() {
25+
String someString = "Just a string";
26+
assumingThat(
27+
someString.equals("Just a string"),
28+
() -> System.out.println("Assumption was correct")
29+
);
30+
}
31+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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(NullPointerException.class, null);
13+
assertEquals(exception.getClass(), NullPointerException.class);
14+
}
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung;
2+
3+
import org.junit.gen5.api.Test;
4+
5+
import static org.junit.gen5.api.Assertions.assertAll;
6+
import static org.junit.gen5.api.Assertions.assertEquals;
7+
import static org.junit.gen5.api.Assertions.assertTrue;
8+
9+
class FirstTest {
10+
11+
@Test
12+
void lambdaExpressions() {
13+
String string = "";
14+
assertTrue(() -> string.isEmpty(), "String should be empty");
15+
}
16+
17+
@Test
18+
void groupAssertions() {
19+
int[] numbers = {0,1,2,3,4};
20+
assertAll("numbers", () -> {
21+
assertEquals(numbers[0], 1);
22+
assertEquals(numbers[3], 3);
23+
assertEquals(numbers[4], 1);
24+
});
25+
}
26+
27+
}
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+
}

0 commit comments

Comments
 (0)