Implicit conversion

Internally, JUnit 5 handles a set of rules for the conversion of parameters from String to the actual argument type. For example, if @ParameterizedTests declares a parameter of type TimeUnit, but the declared source is a String, internally this String will be converted to TimeUnit. The following table summarizes the rules of implicit conversions in JUnit 5 for parameterized test arguments:

Target Type

Example

boolean/Boolean

"false" -> false

byte/Byte

"1" -> (byte) 1

char/Character

"a" -> 'a'

short/Short

"2" -> (short) 2

int/Integer

"3" -> 3

long/Long

"4" -> 4L

float/Float

"5.0" -> 5.0f

double/Double

"6.0" -> 6.0d

Enum subclass

"SECONDS" -> TimeUnit.SECONDS

java.time.Instant

"1970-01-01T00:00:00Z" -> Instant.ofEpochMilli(0)

java.time.LocalDate

"2017-10-24" -> LocalDate.of(2017, 10, 24)

java.time.LocalDateTime

"2017-03-14T12:34:56.789" -> LocalDateTime.of(2017, 3, 14, 12, 34, 56, 789_000_000)

java.time.LocalTime

"12:34:56.789" -> LocalTime.of(12, 34, 56, 789_000_000)

java.time.OffsetDateTime

"2017-03-14T12:34:56.789Z" -> OffsetDateTime.of(2017, 3, 14, 12, 34, 56, 789_000_000, ZoneOffset.UTC)

java.time.OffsetTime

"12:34:56.789Z" -> OffsetTime.of(12, 34, 56, 789_000_000, ZoneOffset.UTC)

java.time.Year

"2017" -> Year.of(2017)

java.time.YearMonth

"2017-10" -> YearMonth.of(2017, 10)

java.time.ZonedDateTime

"2017-10-24T12:34:56.789Z" -> ZonedDateTime.of(2017, 10, 24, 12, 34, 56, 789_000_000, ZoneOffset.UTC)

The following example shows several examples of implicit conversion. The first test (testWithImplicitConversionToBoolean) declares a String source as "true", but then, the expected argument type is Boolean. Similarly, the second test ("testWithImplicitConversionToInteger") makes an implicit conversion from String to Integer. The third test (testWithImplicitConversionToEnum) converts the input String to TimeUnit (enumeration), and finally the fourth test (testWithImplicitConversionToLocalDate) produces a conversion to LocalDate:

package io.github.bonigarcia;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.time.LocalDate;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

class ImplicitConversionParameterizedTest {

@ParameterizedTest
@ValueSource(strings = "true")
void testWithImplicitConversionToBoolean(Boolean argument) {
System.out.println("Argument " + argument + " is a type of "
+ argument.getClass());
assertTrue(argument);
}

@ParameterizedTest
@ValueSource(strings = "11")
void testWithImplicitConversionToInteger(Integer argument) {
System.out.println("Argument " + argument + " is a type of "
+ argument.getClass());
assertTrue(argument > 10);
}

@ParameterizedTest
@ValueSource(strings = "SECONDS")
void testWithImplicitConversionToEnum(TimeUnit argument) {
System.out.println("Argument " + argument + " is a type of "
+ argument.getDeclaringClass());
assertNotNull(argument.name());
}

@ParameterizedTest
@ValueSource(strings = "2017-07-25")
void testWithImplicitConversionToLocalDate(LocalDate argument) {
System.out.println("Argument " + argument + " is a type of "
+ argument.getClass());
assertNotNull(argument);
}

}

We can check the actual type of the argument in the console. Each test writes a line in the standard output with the value and the type of each argument:

Execution of parameterized tests using implicit argument conversion
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.140.204