Skip to content

Instantly share code, notes, and snippets.

@pavelfomin
Last active January 8, 2020 21:38
Show Gist options
  • Save pavelfomin/8234ddcdb4a253d91993f1eff0e66bf2 to your computer and use it in GitHub Desktop.
Save pavelfomin/8234ddcdb4a253d91993f1eff0e66bf2 to your computer and use it in GitHub Desktop.
JUnit 5 Parameterized Test example
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.CsvSource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DateFormatterTest {
DateFormatter formatter = new DateFormatter();
@ParameterizedTest(name = "format {0} using {1}")
@CsvSource({
"2019-01-01, MM/dd/yyyy, 01/01/2019",
"2019-12-31, MM/dd/yyyy, 12/31/2019",
"2019-12-31, dd/MM/yy, 31/12/19"
})
/**
* @param input standard converter is used to create an instance of {@link LocalDate}
*/
void formatDateNotNull(LocalDate input, String pattern, String expected) {
assertEquals(expected, formatter.format(input, pattern));
}
@ParameterizedTest(name = "format {0} using {1}")
@CsvSource({
"null, MM/dd/yyyy, null",
"2019-01-01, MM/dd/yyyy, 01/01/2019",
"2019-12-31, MM/dd/yyyy, 12/31/2019",
"2019-12-31, dd/MM/yy, 31/12/19"
})
/**
* Using custom {@link NullableConverter} null aware converter.
*/
void formatDateWithNull(@ConvertWith(NullableConverter.class) LocalDate input, String pattern, @ConvertWith(NullableConverter.class) String expected) {
assertEquals(expected, formatter.format(input, pattern));
}
@ParameterizedTest(name = "format {0} using {1}")
@CsvSource({
"null, MM/dd/yyyy, null",
"2019-01-01, MM/dd/yyyy, 01/01/2019",
"2019-12-31, MM/dd/yyyy, 12/31/2019",
"2019-12-31, dd/MM/yy, 31/12/19"
})
/**
* Using {@link ArgumentsAccessor} to check for null.
*/
void formatDateWithNull(ArgumentsAccessor arguments) {
LocalDate date = ("null".equals(arguments.getString(0)) ? null : arguments.get(0, LocalDate.class));
String pattern = arguments.getString(1);
String expected = ("null".equals(arguments.getString(2)) ? null : arguments.getString(2));
assertEquals(expected, formatter.format(date, pattern));
}
class DateFormatter {
private String format(LocalDate input, String pattern) {
return input == null ? null : input.format(DateTimeFormatter.ofPattern(pattern));
}
}
}
import org.junit.jupiter.params.converter.ArgumentConversionException;
import org.junit.jupiter.params.converter.DefaultArgumentConverter;
import org.junit.jupiter.params.converter.SimpleArgumentConverter;
/**
* Based on http://dolszewski.com/testing/parameterizedtest-with-null-values-in-cvssource/
*/
public final class NullableConverter extends SimpleArgumentConverter {
@Override
protected Object convert(Object source, Class<?> targetType) throws ArgumentConversionException {
if ("null".equals(source)) {
return null;
}
return DefaultArgumentConverter.INSTANCE.convert(source, targetType);
}
}
<dependencies>
<!--test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
<!-- Parameterized Tests -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.5.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Support for junit 5 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
@pavelfomin
Copy link
Author

I wish null was handled out of the box by junit 5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment