LocalDateTimeSource

@SourceProvider(value = LocalDateTimeValueSourceDataProvider::class)
annotation class LocalDateTimeSource(val values: Array<String>, val dateTimeFormat: String = "yyyy-MM-dd HH:mm")

Annotation to indicate that the annotated LocalDateTime parameter should be populated with a random value from the provided values array.

Samples

/**
 * This will generate 5 tests with the values "2020-01-01", "2021-01-01", "2022-01-01", "2023-01-01", "2024-01-01"
 */
@GeneratedParametersTest
fun testLocalDateTimeSource(
    @LocalDateTimeSource(
        values = [
            "2020-01-01 00:00",
            "2021-01-01 02:00",
            "2022-01-01 04:00",
            "2023-01-01 06:00",
            "2024-01-01 08:00"
        ]
    )
    value: LocalDateTime
) {
    assertThat(value).isLessThan(LocalDateTime.now())
}

/**
 * This will generate 5 tests with the values "01/01/2020", "01/01/2021", "01/01/2022", "01/01/2023", "01/01/2024"
 * The values will be parsed using the format "MM/dd/yyyy"
 */
@GeneratedParametersTest
fun testLocalDateTimeSourceWithFormat(
    @LocalDateTimeSource(
        values = [
            "01/01/2020 00:00:01",
            "01/01/2021 02:00:02",
            "01/01/2022 03:00:03",
            "01/01/2023 04:00:04",
            "01/01/2024 05:00:05"
        ],
        dateTimeFormat = "MM/dd/yyyy hh:mm:ss"
    )
    value: LocalDateTime
) {
    assertThat(value).isLessThan(LocalDateTime.now())
}

Properties

Link copied to clipboard

The format string used to parse the date and time strings in the values array. The default value is "yyyy-MM-dd HH:mm", which represents the format "2023-05-15 14:30".

Link copied to clipboard

An array of strings representing the possible values for the LocalDateTime. The values should be in the format specified by the dateTimeFormat parameter.