... 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.
Without fix, all of the following can fail:
... as
LocalDate.now()
is being used directly inassertEquals
.Let's take TestDayOfMonth.test_now() as an example.
It is very unlikely (although still possible) for local date to change between re-assignment of
DayOfMonth.now()
totest
andLocalDate.now()
call, but it is not so unlikely for local date to change between first assignment ofDayOfMonth.now()
totest
andLocalDate.now()
call.If
LocalDate.now().getDayOfMonth() == test.getValue()
, TestDayOfMonth.test_now() becomes: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.