'Force the local datetime to a given value, in order to perform a JUnit test
In my Java application I have to write an Unit Test and I have to check that this test works in both time of the year Daylight savings time / Normal time. To do that, I should set an hardcoded datetime that override the server datetime; execute my test; and restore the real datetime.
How can I do that? I found this solution https://www.baeldung.com/java-override-system-time but is not what I really need (it only define a Instant entity to use in specific instruction, but I need to fix the desired date time and then to perform a test by using some methods not directly connected to an Instant).
Java.time would be preferred.
Edit
The test assigned to me should only read a data cell from an Excel file (formatted in Excel as Datetime) and make a comparison with a fixed date.
This is a detailed description:
There is an Excel file with a cell formatted as "Datetime"
I read this cell by
Workbook excelWorkbook = new Workbook(excelFileStream, excelOptions); Cells cells = worksheet.getCells();A cell of the sheet if formatted by dateTime
A method in my implementation read the cell and creates a Datetime var
That's all. My task is, create an Unit Test in order to check if the given value is always correct independent from the Daylight saving / Normal time of the year. I should "force" the local date time of the server, implement the test by creating something like
assertThat(ExcelCellDateTime).isEqualTo("<given date time>");
And restore the datetime of the Java application to the correct Date Time of the server. So the question is:
How can I simulate a certain datetime inside a unit test of my Java application, independent from the real date time of the server, such that the application 'believes' that the current datetime is the one I forced and the test can be executed 'as' now would be equal to the datetime I forced?
Comment on possible duplicate
I don't think this is a duplicate of the suggested question. I don't want to force a date and to compare the new date with something else, but I want to force the date and execute a test without the new date as parameter.
Solution 1:[1]
Your business logic should be given a Clock instance to provide the current time (and time zone, when necessary) instead of using static methods like LocalDateTime.now() which hide a dependency on the environment. It's like a pluggable factory for various notions of "now."
When your logic uses a Clock, it is simple for tests to use clocks with a fixed time to test for correct behavior during daylight saving transitions, leap days, and other date-time corner cases.
Well-designed code is easy to use for any purpose, including testing. This sort of difficulty in testing is a symptom of poor design that indicates the code will be hard to reuse in other ways.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | erickson |
