From f27f24d79a5bc85782042986dee75c753253ca54 Mon Sep 17 00:00:00 2001 From: Mincong HUANG Date: Sun, 31 Jan 2021 15:46:43 +0100 Subject: [PATCH 1/5] Create new module JUnit 5 --- junit5/pom.xml | 35 +++++++++++++++++++++++++++++++++++ pom.xml | 1 + 2 files changed, 36 insertions(+) create mode 100644 junit5/pom.xml diff --git a/junit5/pom.xml b/junit5/pom.xml new file mode 100644 index 00000000..bc8031d6 --- /dev/null +++ b/junit5/pom.xml @@ -0,0 +1,35 @@ + + + + java-examples-parent + io.mincong + 1.0.0-SNAPSHOT + + 4.0.0 + + java-examples-junit5 + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + org.junit.jupiter + junit-jupiter-params + test + + + org.assertj + assertj-core + test + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9dba8671..c52cdf1c 100644 --- a/pom.xml +++ b/pom.xml @@ -61,6 +61,7 @@ jetty protobuf reliability + junit5 From 485832c0b5a5f77710080254d17ca102ed1f803f Mon Sep 17 00:00:00 2001 From: Mincong HUANG Date: Sun, 31 Jan 2021 15:47:29 +0100 Subject: [PATCH 2/5] Test multiple implementations using ArgumentsProvider --- .../main/java/io/mincong/junit5/ChatBot.java | 5 +++ .../junit5/StringConcatenationChatBot.java | 9 +++++ .../mincong/junit5/StringFormatChatBot.java | 8 ++++ .../java/io/mincong/junit5/ChatBotTest.java | 39 +++++++++++++++++++ 4 files changed, 61 insertions(+) create mode 100644 junit5/src/main/java/io/mincong/junit5/ChatBot.java create mode 100644 junit5/src/main/java/io/mincong/junit5/StringConcatenationChatBot.java create mode 100644 junit5/src/main/java/io/mincong/junit5/StringFormatChatBot.java create mode 100644 junit5/src/test/java/io/mincong/junit5/ChatBotTest.java diff --git a/junit5/src/main/java/io/mincong/junit5/ChatBot.java b/junit5/src/main/java/io/mincong/junit5/ChatBot.java new file mode 100644 index 00000000..ac958669 --- /dev/null +++ b/junit5/src/main/java/io/mincong/junit5/ChatBot.java @@ -0,0 +1,5 @@ +package io.mincong.junit5; + +public interface ChatBot { + String sayHello(String username); +} diff --git a/junit5/src/main/java/io/mincong/junit5/StringConcatenationChatBot.java b/junit5/src/main/java/io/mincong/junit5/StringConcatenationChatBot.java new file mode 100644 index 00000000..8d24783b --- /dev/null +++ b/junit5/src/main/java/io/mincong/junit5/StringConcatenationChatBot.java @@ -0,0 +1,9 @@ +package io.mincong.junit5; + +public class StringConcatenationChatBot implements ChatBot { + + @Override + public String sayHello(String username) { + return "Hello, " + username; + } +} diff --git a/junit5/src/main/java/io/mincong/junit5/StringFormatChatBot.java b/junit5/src/main/java/io/mincong/junit5/StringFormatChatBot.java new file mode 100644 index 00000000..91c25e68 --- /dev/null +++ b/junit5/src/main/java/io/mincong/junit5/StringFormatChatBot.java @@ -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); + } +} diff --git a/junit5/src/test/java/io/mincong/junit5/ChatBotTest.java b/junit5/src/test/java/io/mincong/junit5/ChatBotTest.java new file mode 100644 index 00000000..20d40d15 --- /dev/null +++ b/junit5/src/test/java/io/mincong/junit5/ChatBotTest.java @@ -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 provideArguments(ExtensionContext context) { + return Stream.of(new StringFormatChatBot(), new StringConcatenationChatBot()) + .map(Arguments::of); + } + } +} From 8b100123365f66499e72ef13bc6a6cf0345bac39 Mon Sep 17 00:00:00 2001 From: Mincong HUANG Date: Sun, 31 Jan 2021 15:49:44 +0100 Subject: [PATCH 3/5] Improve description --- README.md | 3 ++- junit5/pom.xml | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index cd4ca9be..338d5635 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/junit5/pom.xml b/junit5/pom.xml index bc8031d6..3bcda980 100644 --- a/junit5/pom.xml +++ b/junit5/pom.xml @@ -10,6 +10,8 @@ 4.0.0 java-examples-junit5 + Java Examples - JUnit 5 + org.junit.jupiter From 18ae320e4debbc80705d715df6a7528059c05b4d Mon Sep 17 00:00:00 2001 From: Mincong HUANG Date: Sun, 31 Jan 2021 16:05:45 +0100 Subject: [PATCH 4/5] Add a untested method --- .../io/mincong/junit5/StringConcatenationChatBot.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/junit5/src/main/java/io/mincong/junit5/StringConcatenationChatBot.java b/junit5/src/main/java/io/mincong/junit5/StringConcatenationChatBot.java index 8d24783b..bfee123c 100644 --- a/junit5/src/main/java/io/mincong/junit5/StringConcatenationChatBot.java +++ b/junit5/src/main/java/io/mincong/junit5/StringConcatenationChatBot.java @@ -6,4 +6,14 @@ public class StringConcatenationChatBot implements ChatBot { 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; + } } From 9e0f4fe13bd1cf10a891c939cf286d3cbc510021 Mon Sep 17 00:00:00 2001 From: Mincong HUANG Date: Sun, 31 Jan 2021 21:16:23 +0100 Subject: [PATCH 5/5] Create MathTest --- .../test/java/io/mincong/junit5/MathTest.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 junit5/src/test/java/io/mincong/junit5/MathTest.java diff --git a/junit5/src/test/java/io/mincong/junit5/MathTest.java b/junit5/src/test/java/io/mincong/junit5/MathTest.java new file mode 100644 index 00000000..0d3e2582 --- /dev/null +++ b/junit5/src/test/java/io/mincong/junit5/MathTest.java @@ -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); + } +}