Tuesday 27 July 2021

Junit5: Argument Conversion

Junit5 supports automatic arguments conversion. There are three types of conversions.

a.   Widening Conversion

b.   Implicit Conversion

c.    Explicit Conversion

 

Widening Conversion

Junit supports widening of primitive types that are supplied to @ParameterizedTest.

 

Example

byte to short, int, long, float, or double

short to int, long, float, or double

char to int, long, float, or double

int to long, float, or double

long to float or double

float to double

 

For example, a parameterized test annotated with @ValueSource(ints = { 1, 2, 3 }) can be declared to accept not only an argument of type int but also an argument of type long, float, or double.

 

Reference

https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.2

 

Implicit Conversion

Junit has number of built-in implicit type converters.

 

Below table summarizes how the implicit type conversion happen.

 

Target Type

Example

boolean/Boolean

"true"  true

byte/Byte

"15""0xF", or "017"  (byte) 15

char/Character

"o"  'o'

short/Short

"15""0xF", or "017"  (short) 15

int/Integer

"15""0xF", or "017"  15

long/Long

"15""0xF", or "017"  15L

float/Float

"1.0"  1.0f

double/Double

"1.0"  1.0d

Enum subclass

"SECONDS"  TimeUnit.SECONDS

java.io.File

"/path/to/file"  new File("/path/to/file")

java.lang.Class

"java.lang.Integer"  java.lang.Integer.class (use $ for nested classes, e.g. "java.lang.Thread$State")

java.lang.Class

"byte"  byte.class (primitive types are supported)

java.lang.Class

"char[]"  char[].class (array types are supported)

java.math.BigDecimal

"123.456e789"  new BigDecimal("123.456e789")

java.math.BigInteger

"1234567890123456789"  new BigInteger("1234567890123456789")

java.net.URI

"https://junit.org/"  URI.create("https://junit.org/")

java.net.URL

"https://junit.org/"  new URL("https://junit.org/")

java.nio.charset.Charset

"UTF-8"  Charset.forName("UTF-8")

java.nio.file.Path

"/path/to/file"  Paths.get("/path/to/file")

java.time.Duration

"PT3S"  Duration.ofSeconds(3)

java.time.Instant

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

java.time.LocalDateTime

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

java.time.LocalDate

"2017-03-14"  LocalDate.of(2017, 3, 14)

java.time.LocalTime

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

java.time.MonthDay

"--03-14"  MonthDay.of(3, 14)

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.Period

"P2M6D"  Period.of(0, 2, 6)

java.time.YearMonth

"2017-03"  YearMonth.of(2017, 3)

java.time.Year

"2017"  Year.of(2017)

java.time.ZonedDateTime

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

java.time.ZoneId

"Europe/Berlin"  ZoneId.of("Europe/Berlin")

java.time.ZoneOffset

"+02:30"  ZoneOffset.ofHoursMinutes(2, 30)

java.util.Currency

"JPY"  Currency.getInstance("JPY")

java.util.Locale

"en"  new Locale("en")

java.util.UUID

"d043e930-7b3b-48e3-bdbe-5a3ccfb833db"  UUID.fromString("d043e930-7b3b-48e3-bdbe-5a3ccfb833db")

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment