Testing the correct processing of the time zone

We work with large volumes of data, all of them are marked in UTC (in Java). Between reading this data, storing it in a database and retrieving it again, it happened that some of the data was turned off for one hour in the summer. Since UTC has no idea about daylight saving time, this was obviously a software bug. Once this is known, it is easy to fix.

However, it would be nice to have some modulation / integration tests that work regardless of the current time difference - for example. I would like to change the local time zone and repeat some methods over and over in these different time zones to ensure that UTC is processed correctly.

Since the tests should run automatically and - preferably - in one Testsuite, I wonder how best to check the correct behavior. It would be easy to change local settings, such as the time zone, after restarting the JVM, but doing this in a test suite is not so simple.

Does anyone know of a test environment, library, or template that supports this scenario? We usually work with JUnit, but are open to adding another environment / technique if this helps to get rid of such problems. I believe this is integration rather than unit test.

Edit : There are already two very useful answers, but I think there should be more technology there. Does anyone have authoritative information about when / how often TimeZone.getDefault is called (see comments for John Skets's answers)?

Note Even if this question has an accepted answer, I was not completely sure which answer to accept. Even with this agreement, I would like to see more ideas and methods.

Thanks for your input!

+6
java timezone utc
source share
3 answers

I would recommend you check out JodaTime , which contains a few sugars that help manage Date / Time / TimeZone types more clearly in your code.

We use them throughout the test and production, as it raises our own Java API for date / time problems, it is unparalleled. Using these tests works fine in JUnit

+6
source share

Java allows you to set the default time zone (java.util.TimeZone.setDefault). I already wrote tests to set the time zone for various parameters and verify that it still works. Be careful though, if you are parallelizing most of your unit tests, you need to make them consistent.

I suggest you test a few hours with daylight saving time, and some without. Using the Australian time zone is also good, as DST is applied at the opposite time of the year to the northern hemisphere.

+7
source share

What is your opinion on connecting to time servers and getting updates?

+1
source share

All Articles