Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ Jetty | Jetty Server.
JGit | Basic usages of [JGit][jgit].
JMH | Java Microbenchmark Harness (JMH).
JSON | JSON conversion libraries in Java.
JUnit | JUnit testing framework.
JUnit 4 | JUnit 4 testing framework.
JUnit 5 | JUnit 5 testing framework.
Logback | [Logback](http://logback.qos.ch/) logging framework.
Maven | Basic functionality of Maven.
Mongo | The MongoDB database
Expand Down
37 changes: 37 additions & 0 deletions junit5/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>java-examples-parent</artifactId>
<groupId>io.mincong</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>java-examples-junit5</artifactId>
<name>Java Examples - JUnit 5</name>

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
5 changes: 5 additions & 0 deletions junit5/src/main/java/io/mincong/junit5/ChatBot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.mincong.junit5;

public interface ChatBot {
String sayHello(String username);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.mincong.junit5;

public class StringConcatenationChatBot implements ChatBot {

@Override
public String sayHello(String username) {
return "Hello, " + username;
}

/**
* In IntelliJ IDEA, run ChatBotTest with coverage and observe the test coverage here. You can see
* that this test is not tested. One advantage of using parameterized testing is that it can
* increase the test coverage easily with difference scenarios.
*/
@SuppressWarnings("unused")
public String sayNo(String username) {
return "No, " + username;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.mincong.junit5;

public class StringFormatChatBot implements ChatBot {
@Override
public String sayHello(String username) {
return String.format("Hello, %s", username);
}
}
39 changes: 39 additions & 0 deletions junit5/src/test/java/io/mincong/junit5/ChatBotTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.mincong.junit5;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.stream.Stream;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.ArgumentsProvider;
import org.junit.jupiter.params.provider.ArgumentsSource;

class ChatBotTest {
/**
* Use annotation {@link ParameterizedTest} to test multiple implementation of {@link ChatBot}. It
* ensures that all implementations respect the specification of the interface and returns the
* expected results regardless the internal implementation.
*
* @param bot the chat bot to test
*/
@ParameterizedTest
@ArgumentsSource(ChatBotProvider.class)
void sayHello(ChatBot bot) {
assertThat(bot.sayHello("Foo")).isEqualTo("Hello, Foo");
assertThat(bot.sayHello("Bar")).isEqualTo("Hello, Bar");
}

public static class ChatBotProvider implements ArgumentsProvider {

/**
* This method creates multiple chat bot instances using different implementations and returns
* them as a stream of arguments for the parameterized test.
*/
@Override
public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
return Stream.of(new StringFormatChatBot(), new StringConcatenationChatBot())
.map(Arguments::of);
}
}
}
18 changes: 18 additions & 0 deletions junit5/src/test/java/io/mincong/junit5/MathTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.mincong.junit5;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class MathTest {
@ParameterizedTest
@CsvSource({
"1, 2, 2",
"1, -1, 1",
"1, 1, 1",
})
void testMax(int a, int b, int max) {
assertThat(Math.max(a, b)).isEqualTo(max);
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<module>jetty</module>
<module>protobuf</module>
<module>reliability</module>
<module>junit5</module>
</modules>

<dependencyManagement>
Expand Down