Skip to content

Commit

Permalink
Configure Surefire plugin to use UTC as default time zone (#359)
Browse files Browse the repository at this point in the history
* In pom.xml set the JVM time zone to UTC when running tests
* Log a warning in AbstractApplicationErrorDaoTest when the
  timestamp assertions fail, explaining that it is probably
  a timezone problem. This should only occur when using something
  other than Maven and IntelliJ to run tests, such as VSCode
  which doesn't apply the Surefire plugin configuration when
  running tests via its test runner. IntelliJ is smarter
  and automatically applies the configuration when running
  tests via its test runner.
  • Loading branch information
sleberknight authored Feb 18, 2024
1 parent 1a40e65 commit 46f13dd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,26 @@

</dependencies>

<build>
<plugins>

<!--
Set the default time zone to UTC when running tests.
Otherwise, certain tests will fail due to differences
in how some databases handle time zones.
For example, MySQL and SQLite handle them differently
from Postgres and H2.
-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>-Duser.timezone=UTC</argLine>
</configuration>
</plugin>

</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.kiwiproject.collect.KiwiLists.first;
import static org.kiwiproject.test.util.DateTimeTestHelper.assertTimeDifferenceWithinTolerance;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.assertj.core.api.SoftAssertions;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
Expand All @@ -24,6 +25,7 @@
import java.io.UncheckedIOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.List;
Expand All @@ -35,6 +37,7 @@
*/
@ExtendWith(ApplicationErrorExtension.class)
@ExtendWith(SoftAssertionsExtension.class)
@Slf4j
public abstract class AbstractApplicationErrorDaoTest<T extends ApplicationErrorDao> {

private T errorDao;
Expand Down Expand Up @@ -165,6 +168,13 @@ void shouldInsertNewApplicationErrorRecord(SoftAssertions softly) {
var oneSecondInMillis = 1_000;
assertTimeDifferenceWithinTolerance(softly, "createdAt", beforeInsert, retrievedError.getCreatedAt(), oneSecondInMillis);
assertTimeDifferenceWithinTolerance(softly, "updatedAt", beforeInsert, retrievedError.getUpdatedAt(), oneSecondInMillis);
if (!softly.wasSuccess()) {
LOG.warn("When the time difference assertions fail, it is probably due to a time zone difference." +
" This library currently requires UTC for the JVM and database." +
" The JVM system default time zone is {}." +
" The database time zone is most likely using the same default time zone.",
ZoneId.systemDefault());
}
softly.assertThat(retrievedError.getDescription()).isEqualTo(description);
softly.assertThat(retrievedError.getExceptionType()).isEqualTo(throwable.getClass().getName());
softly.assertThat(retrievedError.getExceptionMessage()).isEqualTo(throwable.getMessage());
Expand Down

0 comments on commit 46f13dd

Please sign in to comment.