ThreeTen / threeten-extra

Provides additional date-time classes that complement those in JDK 8
http://www.threeten.org/threeten-extra/
BSD 3-Clause "New" or "Revised" License
393 stars 77 forks source link

Fix test_now*() in TestDayOfMonth and TestDayOfYear #218

Closed perceptron8 closed 2 years ago

perceptron8 commented 2 years ago

Without fix, all of the following can fail:

... as LocalDate.now() is being used directly in assertEquals.


Let's take TestDayOfMonth.test_now() as an example.

@Test
public void test_now() {
    DayOfMonth test = DayOfMonth.now();
    if (LocalDate.now().getDayOfMonth() != test.getValue()) {
        test = DayOfMonth.now();
    }
    assertEquals(LocalDate.now().getDayOfMonth(), test.getValue());
}

It is very unlikely (although still possible) for local date to change between re-assignment of DayOfMonth.now() to test and LocalDate.now() call, but it is not so unlikely for local date to change between first assignment of DayOfMonth.now() to test and LocalDate.now() call.

If LocalDate.now().getDayOfMonth() == test.getValue(), TestDayOfMonth.test_now() becomes:

@Test
public void test_now() {
    DayOfMonth test = DayOfMonth.now();
    assertEquals(LocalDate.now().getDayOfMonth(), test.getValue());
}

It's clear, that local date can change between line 1 and 2.


This PR introduces https://junit-pioneer.org/docs/retrying-test/ to fix 4 flawed tests and also improves readability of few others.