Skip to content

Commit e871f15

Browse files
authored
Add module JUnit 5 (#182)
* Create new module JUnit 5 * Test multiple implementations using ArgumentsProvider * Improve description * Add a untested method
2 parents 21dd37c + 9e0f4fe commit e871f15

File tree

8 files changed

+129
-1
lines changed

8 files changed

+129
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ Jetty | Jetty Server.
2727
JGit | Basic usages of [JGit][jgit].
2828
JMH | Java Microbenchmark Harness (JMH).
2929
JSON | JSON conversion libraries in Java.
30-
JUnit | JUnit testing framework.
30+
JUnit 4 | JUnit 4 testing framework.
31+
JUnit 5 | JUnit 5 testing framework.
3132
Logback | [Logback](http://logback.qos.ch/) logging framework.
3233
Maven | Basic functionality of Maven.
3334
Mongo | The MongoDB database

junit5/pom.xml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
<parent>
6+
<artifactId>java-examples-parent</artifactId>
7+
<groupId>io.mincong</groupId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>java-examples-junit5</artifactId>
13+
<name>Java Examples - JUnit 5</name>
14+
15+
<dependencies>
16+
<dependency>
17+
<groupId>org.junit.jupiter</groupId>
18+
<artifactId>junit-jupiter-api</artifactId>
19+
<scope>test</scope>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.junit.jupiter</groupId>
23+
<artifactId>junit-jupiter-engine</artifactId>
24+
<scope>test</scope>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.junit.jupiter</groupId>
28+
<artifactId>junit-jupiter-params</artifactId>
29+
<scope>test</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.assertj</groupId>
33+
<artifactId>assertj-core</artifactId>
34+
<scope>test</scope>
35+
</dependency>
36+
</dependencies>
37+
</project>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.mincong.junit5;
2+
3+
public interface ChatBot {
4+
String sayHello(String username);
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package io.mincong.junit5;
2+
3+
public class StringConcatenationChatBot implements ChatBot {
4+
5+
@Override
6+
public String sayHello(String username) {
7+
return "Hello, " + username;
8+
}
9+
10+
/**
11+
* In IntelliJ IDEA, run ChatBotTest with coverage and observe the test coverage here. You can see
12+
* that this test is not tested. One advantage of using parameterized testing is that it can
13+
* increase the test coverage easily with difference scenarios.
14+
*/
15+
@SuppressWarnings("unused")
16+
public String sayNo(String username) {
17+
return "No, " + username;
18+
}
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package io.mincong.junit5;
2+
3+
public class StringFormatChatBot implements ChatBot {
4+
@Override
5+
public String sayHello(String username) {
6+
return String.format("Hello, %s", username);
7+
}
8+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package io.mincong.junit5;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.stream.Stream;
6+
import org.junit.jupiter.api.extension.ExtensionContext;
7+
import org.junit.jupiter.params.ParameterizedTest;
8+
import org.junit.jupiter.params.provider.Arguments;
9+
import org.junit.jupiter.params.provider.ArgumentsProvider;
10+
import org.junit.jupiter.params.provider.ArgumentsSource;
11+
12+
class ChatBotTest {
13+
/**
14+
* Use annotation {@link ParameterizedTest} to test multiple implementation of {@link ChatBot}. It
15+
* ensures that all implementations respect the specification of the interface and returns the
16+
* expected results regardless the internal implementation.
17+
*
18+
* @param bot the chat bot to test
19+
*/
20+
@ParameterizedTest
21+
@ArgumentsSource(ChatBotProvider.class)
22+
void sayHello(ChatBot bot) {
23+
assertThat(bot.sayHello("Foo")).isEqualTo("Hello, Foo");
24+
assertThat(bot.sayHello("Bar")).isEqualTo("Hello, Bar");
25+
}
26+
27+
public static class ChatBotProvider implements ArgumentsProvider {
28+
29+
/**
30+
* This method creates multiple chat bot instances using different implementations and returns
31+
* them as a stream of arguments for the parameterized test.
32+
*/
33+
@Override
34+
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
35+
return Stream.of(new StringFormatChatBot(), new StringConcatenationChatBot())
36+
.map(Arguments::of);
37+
}
38+
}
39+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.mincong.junit5;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.params.ParameterizedTest;
6+
import org.junit.jupiter.params.provider.CsvSource;
7+
8+
public class MathTest {
9+
@ParameterizedTest
10+
@CsvSource({
11+
"1, 2, 2",
12+
"1, -1, 1",
13+
"1, 1, 1",
14+
})
15+
void testMax(int a, int b, int max) {
16+
assertThat(Math.max(a, b)).isEqualTo(max);
17+
}
18+
}

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<module>jetty</module>
6262
<module>protobuf</module>
6363
<module>reliability</module>
64+
<module>junit5</module>
6465
</modules>
6566

6667
<dependencyManagement>

0 commit comments

Comments
 (0)