Merge pull request #15533 from lsnare/BAEL-7271-junit-parameterresolver
BAEL-7271: Solving the ParameterResolutionException in JUnit 5
This commit is contained in:
commit
984b1ac6e7
|
@ -0,0 +1,3 @@
|
|||
## Relevant Articles:
|
||||
|
||||
- [Solving the ParameterResolutionException in JUnit 5](https://www.baeldung.com/solving-the-parameterresolutionexception-in-junit-5/)
|
|
@ -0,0 +1,82 @@
|
|||
<?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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>junit-5-advanced-2</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>junit-5-advanced-2</name>
|
||||
<description>Advanced JUnit 5 Topics</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>testing-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.jmockit</groupId>
|
||||
<artifactId>jmockit</artifactId>
|
||||
<version>${jmockit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-suite</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-runner</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-launcher</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-commons</artifactId>
|
||||
<version>${junit-platform.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>
|
||||
-javaagent:${settings.localRepository}/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
|
||||
</argLine>
|
||||
<disableXmlReport>true</disableXmlReport>
|
||||
<excludes>
|
||||
<exclude>**/testsuite/**/*UnitTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<jmockit.version>1.49</jmockit.version>
|
||||
<assertj.version>3.24.2</assertj.version>
|
||||
<junit-platform.version>1.10.1</junit-platform.version>
|
||||
<maven-surefire-plugin.version>3.0.0</maven-surefire-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -27,6 +27,7 @@
|
|||
<module>jqwik</module>
|
||||
<module>junit-4</module>
|
||||
<module>junit-5-advanced</module>
|
||||
<module>junit-5-advanced-2</module>
|
||||
<module>junit-5-basics</module>
|
||||
<module>junit-5-basics-2</module>
|
||||
<module>junit-5</module>
|
||||
|
|
Loading…
Reference in New Issue