Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Junit5 to receive the LocalDate value as params?

Tags:

java

junit5

Last few days I learned the Junit5 in offical. And Recently I want to use it as my test instance,But I meet a difficulty.

I try to let LocalDate value as the params,But I have tried all of my knowledge to do it and all failed.

this is my code

    @ParameterizedTest
@CsvSource({"2020-2-2,2010-2-3"})
    void t5_1(LocalDate a, LocalDate b) {
        final Duration between = Duration.between(a, b);
        System.out.println(between.toDays());
    }
    static Stream<Arguments>timeArrayProvider() {
        return Stream.of(
                Arguments.of((Object) new String[]{"2020-2-2","2010-2-3"})
        );
    }

If you know it ,Thank you in advance.

like image 984
user16838235 Avatar asked Oct 21 '25 16:10

user16838235


1 Answers

I found the solution that uses the Default Converter:

@ParameterizedTest
@CsvSource({"true, 3.14159265359, AUGUST, 2018, 2018-08-23T22:00:00"})
void testDefaultConverters(
    boolean b, double d, Summer s, Year y, LocalDateTime dt) { }

enum Summer {
    JUNE, JULY, AUGUST, SEPTEMBER;
}

If your dates don't come in ISO 8601, @JavaTimeConversionPattern helps you out:

@ParameterizedTest
@CsvSource({"true, 3.14159265359, AUGUST, 2018, 23.08.2018"})
void testDefaultConverters(
    boolean b, double d, Summer s, Year y,
    @JavaTimeConversionPattern("dd.MM.yyyy") LocalDate dt) { }

Kudos to Nicolai Parlog: https://nipafx.dev/junit-5-parameterized-tests/

like image 59
Renan Franca Avatar answered Oct 24 '25 05:10

Renan Franca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!