I've made a unit-test of a Service, when I execute the Jenkins job, every test method pass correctly, except one.
But this test method, works on my machine, both with Eclipse and using the mvn
command.
// TARGET_RUN_DATE_OF_YEAR = "2018-01-01"
@Test
public void dateToTimestamp() {
Service service = getService();
String df = "YYYY-MM-dd";
String invalid = "INVALID";
// Check success
Timestamp timestamp = service.dateToTimestamp(TARGET_RUN_DATE_OF_YEAR, df);
Assert.assertEquals(service.getTodayTimestamp(), timestamp); // <-- Fail here
// Check failure
Assert.assertNull(service.dateToTimestamp(TARGET_RUN_DATE_OF_YEAR, invalid));
Assert.assertNull(service.dateToTimestamp(invalid, df));
}
The Service have mulitple methods:
getTodayTimestamp
give today Timestamp, this method is tested, it works on my machine and on Jenkins.dateToTimestamp
takes a date and a dateFormat as Strings, and returns the date as a Timestamp, this method is the one not working.
The dateToTimestamp
method in the Service:
private Timestamp dateToTimestamp(String date, DateFormat df) throws ParseException {
return new Timestamp(df.parse(date).getTime());
}
@Override
public Timestamp dateToTimestamp(String date, String dateFormatString) {
try {
DateFormat df = new SimpleDateFormat(dateFormatString);
return dateToTimestamp(date, df);
} catch (Exception e) {
log.warn("Exception during conversion of date to timestamp, exception : {}", e);
return null;
}
}
As I previously said, the test work perfectly on my pc, but not on Jenkins (adding the @Ignore
annotations to this method, makes the job successful).
When launching the job, I get this error:
Failed tests: dateToTimestamp(com.test.service.ServiceImplTest): expected:<2018-01-01 00:00:00.0> but was:<2017-12-31 00:00:00.0>
What I can assure, is that even in Jenkins, the dateToTimestamp
method takes the parameters TARGET_RUN_DATE_OF_YEAR
, which is "2018-01-01"
and the dateFormart String as "YYYY-MM-dd"
. But still returns 2017-12-31 00:00:00.0
as Timestamp.
Any ideas?