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.