RandomInstantSource
This annotation is used to generate random java.time.Instants. The min and max values are used to determine the range of values to generate. If useOffset is true, the min and max values are treated as offsets from the current time. Otherwise, the min and max values are treated as java.time.Instants. The size parameter determines the number of random values to generate. The truncateTo parameter determines the truncation unit that the starting instant will be truncated to.
Samples
import assertk.Assert
import assertk.all
import assertk.assertThat
import assertk.assertions.isBetween
import assertk.assertions.isIn
import assertk.assertions.isLessThan
import assertk.assertions.support.expected
import com.wesleyhome.test.jupiter.annotations.GeneratedParametersTest
import com.wesleyhome.test.jupiter.annotations.StringSource
import com.wesleyhome.test.jupiter.annotations.datetime.InstantRangeSource
import com.wesleyhome.test.jupiter.annotations.datetime.InstantSource
import com.wesleyhome.test.jupiter.annotations.datetime.LocalDateRangeSource
import com.wesleyhome.test.jupiter.annotations.datetime.LocalDateSource
import com.wesleyhome.test.jupiter.annotations.datetime.LocalDateTimeRangeSource
import com.wesleyhome.test.jupiter.annotations.datetime.LocalDateTimeSource
import com.wesleyhome.test.jupiter.annotations.datetime.LocalTimeRangeSource
import com.wesleyhome.test.jupiter.annotations.datetime.LocalTimeSource
import com.wesleyhome.test.jupiter.annotations.datetime.RandomInstantSource
import com.wesleyhome.test.jupiter.annotations.number.DoubleRangeSource
import com.wesleyhome.test.jupiter.annotations.number.DoubleSource
import com.wesleyhome.test.jupiter.annotations.number.FloatRangeSource
import com.wesleyhome.test.jupiter.annotations.number.FloatSource
import com.wesleyhome.test.jupiter.annotations.number.IntRangeSource
import com.wesleyhome.test.jupiter.annotations.number.IntSource
import com.wesleyhome.test.jupiter.annotations.number.LongRangeSource
import com.wesleyhome.test.jupiter.annotations.number.LongSource
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.temporal.ChronoUnit
fun main() {
//sampleStart
/**
* This will generate 10 tests with random Instant values
* between 2023-01-01T00:00:00.000Z and 2023-01-02T01:00:00.000Z
*/
@GeneratedParametersTest
fun testRandomInstantSourceWithInstant(
@RandomInstantSource(
size = 10,
min = "2023-01-01T00:00:00.000Z",
max = "2023-01-02T01:00:00.000Z"
)
value: Instant
) {
assertThat(value).isLessThan(Instant.now())
}
@GeneratedParametersTest
fun testRandomInstantSourceWithOffset(
@RandomInstantSource(
size = 10,
min = "PT1H",
max = "PT2H",
useOffset = true
)
value: Instant
) {
assertThat(value).isBetween(
Instant.now().plus(1, ChronoUnit.HOURS),
Instant.now().plus(2, ChronoUnit.HOURS)
)
}
//sampleEnd
}Properties
The maximum value that is used to generate random values. If useOffset is true, this value needs to be parsable to a java.time.Period Otherwise this value needs to be parseable to a java.time.Instant
This minimum value that is used to generate random values. If useOffset is true, this value needs to be parsable to a java.time.Period Otherwise this value needs to be parseable to a java.time.Instant
The truncation unit that the starting instant will be truncated to. This is only used if minOffset is provided.