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..3a98488398 --- /dev/null +++ b/testing-modules/junit-5-advanced-2/README.md @@ -0,0 +1,3 @@ +## Relevant Articles: + +- [Solving the ParameterResolutionException in JUnit 5](https://www.baeldung.com/solving-the-parameterresolutionexception-in-junit-5/) 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 ad20a65afc..8246dae7db 100644 --- a/testing-modules/pom.xml +++ b/testing-modules/pom.xml @@ -27,6 +27,7 @@ jqwik junit-4 junit-5-advanced + junit-5-advanced-2 junit-5-basics junit-5-basics-2 junit-5