From 32594a2b23e3e408630576941bf071aeca9b04b5 Mon Sep 17 00:00:00 2001 From: Lucian Snare Date: Mon, 1 Jan 2024 22:43:51 -0500 Subject: [PATCH 1/2] Add examples of failing parameterized tests with no ParameterResolver --- testing-modules/junit-5-advanced-2/README.md | 3 + testing-modules/junit-5-advanced-2/pom.xml | 82 +++++++++++++++++++ .../com/baeldung/parameterresolver/Book.java | 45 ++++++++++ .../BookParameterResolver.java | 20 +++++ .../ParameterResolverExceptionUnitTest.java | 35 ++++++++ testing-modules/pom.xml | 1 + 6 files changed, 186 insertions(+) create mode 100644 testing-modules/junit-5-advanced-2/README.md create mode 100644 testing-modules/junit-5-advanced-2/pom.xml create mode 100644 testing-modules/junit-5-advanced-2/src/main/java/com/baeldung/parameterresolver/Book.java create mode 100644 testing-modules/junit-5-advanced-2/src/test/java/com/baeldung/parameterresolver/BookParameterResolver.java create mode 100644 testing-modules/junit-5-advanced-2/src/test/java/com/baeldung/parameterresolver/ParameterResolverExceptionUnitTest.java diff --git a/testing-modules/junit-5-advanced-2/README.md b/testing-modules/junit-5-advanced-2/README.md new file mode 100644 index 0000000000..cad6314154 --- /dev/null +++ b/testing-modules/junit-5-advanced-2/README.md @@ -0,0 +1,3 @@ +## Relevant Articles: + +- []() diff --git a/testing-modules/junit-5-advanced-2/pom.xml b/testing-modules/junit-5-advanced-2/pom.xml new file mode 100644 index 0000000000..d2aab09c5a --- /dev/null +++ b/testing-modules/junit-5-advanced-2/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + junit-5-advanced-2 + 1.0-SNAPSHOT + junit-5-advanced-2 + Advanced JUnit 5 Topics + + + com.baeldung + testing-modules + 1.0.0-SNAPSHOT + + + + + org.jmockit + jmockit + ${jmockit.version} + test + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.junit.platform + junit-platform-suite + ${junit-platform.version} + test + + + org.junit.platform + junit-platform-runner + ${junit-platform.version} + test + + + org.junit.platform + junit-platform-launcher + ${junit-platform.version} + test + + + org.junit.platform + junit-platform-commons + ${junit-platform.version} + test + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + -javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar + + true + + **/testsuite/**/*UnitTest.java + + + + + + + + 1.49 + 3.24.2 + 1.10.1 + 3.0.0 + + + \ No newline at end of file diff --git a/testing-modules/junit-5-advanced-2/src/main/java/com/baeldung/parameterresolver/Book.java b/testing-modules/junit-5-advanced-2/src/main/java/com/baeldung/parameterresolver/Book.java new file mode 100644 index 0000000000..bbcf93a24c --- /dev/null +++ b/testing-modules/junit-5-advanced-2/src/main/java/com/baeldung/parameterresolver/Book.java @@ -0,0 +1,45 @@ +package com.baeldung.parameterresolver; + +import java.util.Objects; + +public class Book { + + private String title; + private String author; + + public Book(String title, String author) { + this.title = title; + this.author = author; + } + + @Override + public boolean equals(Object o) { + if (this == o) + return true; + if (!(o instanceof Book)) + return false; + Book book = (Book) o; + return Objects.equals(title, book.title) && Objects.equals(author, book.author); + } + + @Override + public int hashCode() { + return Objects.hash(title, author); + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getAuthor() { + return author; + } + + public void setAuthor(String author) { + this.author = author; + } +} diff --git a/testing-modules/junit-5-advanced-2/src/test/java/com/baeldung/parameterresolver/BookParameterResolver.java b/testing-modules/junit-5-advanced-2/src/test/java/com/baeldung/parameterresolver/BookParameterResolver.java new file mode 100644 index 0000000000..a531dd91bb --- /dev/null +++ b/testing-modules/junit-5-advanced-2/src/test/java/com/baeldung/parameterresolver/BookParameterResolver.java @@ -0,0 +1,20 @@ +package com.baeldung.parameterresolver; + +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.ParameterContext; +import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.jupiter.api.extension.ParameterResolver; + +public class BookParameterResolver implements ParameterResolver { + @Override + public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return parameterContext.getParameter().getType() == Book.class; + } + + @Override + public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException { + return parameterContext.getParameter().getType() == Book.class + ? new Book("Wuthering Heights", "Charlotte Bronte") + : null; + } +} diff --git a/testing-modules/junit-5-advanced-2/src/test/java/com/baeldung/parameterresolver/ParameterResolverExceptionUnitTest.java b/testing-modules/junit-5-advanced-2/src/test/java/com/baeldung/parameterresolver/ParameterResolverExceptionUnitTest.java new file mode 100644 index 0000000000..7af3d62ff3 --- /dev/null +++ b/testing-modules/junit-5-advanced-2/src/test/java/com/baeldung/parameterresolver/ParameterResolverExceptionUnitTest.java @@ -0,0 +1,35 @@ +package com.baeldung.parameterresolver; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.CsvSource; + +@ExtendWith(BookParameterResolver.class) +public class ParameterResolverExceptionUnitTest { + + @Test + void givenBook_whenConfiguringParameterResolver_thenParameterResolves(Book book) { + assertThat(book.getTitle()).isEqualTo("Wuthering Heights"); + } + + // Uncomment these annotations to reproduce the ParameterResolutionException + // @ParameterizedTest + // @CsvSource({"Wuthering Heights, Charlotte Bronte", "Jane Eyre, Charlotte Bronte"}) + // @Test + void givenParameterizedTest_whenUsingTestAnnotation_thenParameterFailsToResolve(String title, String author) { + Book book = new Book(title, author); + assertThat(book.getTitle().length()).isGreaterThan(0); + assertThat(book.getAuthor().length()).isGreaterThan(0); + } + + // Uncomment these annotations to reproduce the ParameterResolutionException + // @Test + void givenBasicTest_whenPassingParameters_thenParameterFailsToResolve(String title, String author) { + Book book = new Book(title, author); + assertThat(book.getTitle().length()).isGreaterThan(0); + assertThat(book.getAuthor().length()).isGreaterThan(0); + } +} diff --git a/testing-modules/pom.xml b/testing-modules/pom.xml index b2e73570d5..103bcd67f3 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -26,6 +26,7 @@ jqwik junit-4 junit-5-advanced + junit-5-advanced-2 junit-5-basics junit-5-basics-2 junit-5 From eb8f8cf56d77ebc2e52bfb962d2e11e742c224a8 Mon Sep 17 00:00:00 2001 From: Lucian Snare Date: Mon, 1 Jan 2024 23:28:48 -0500 Subject: [PATCH 2/2] Update readme --- testing-modules/junit-5-advanced-2/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/testing-modules/junit-5-advanced-2/README.md b/testing-modules/junit-5-advanced-2/README.md index cad6314154..3a98488398 100644 --- a/testing-modules/junit-5-advanced-2/README.md +++ b/testing-modules/junit-5-advanced-2/README.md @@ -1,3 +1,3 @@ ## Relevant Articles: -- []() +- [Solving the ParameterResolutionException in JUnit 5](https://www.baeldung.com/solving-the-parameterresolutionexception-in-junit-5/)