Lukáš Šálek Lukáš Šálek - 24 days ago 9
Android Question

BuddyBuild tests fail, but on local works fine

Hi I have problem with tests build on BuddyBuild.

Method what i want to test:

public static String convertTimeToDepartureDate(long timeInMillis) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(timeInMillis);
SimpleDateFormat format = new SimpleDateFormat("d MMM HH:mm", Locale.ENGLISH);
return format.format(calendar.getTime());
}


and this is my test:

@Test
public void timeToDepartureTest() throws Exception {
String dateDeparture = TimeUtils.convertTimeToDepartureDate(timeMillis);
assertEquals("Date have wrong format or numbers!", "7 Oct 16:45", dateDeparture);
}


And this is one solution what i found on the internet but doesn't work:

@Test
public void timeToDepartureTest() throws Exception {
synchronized (Calendar.getInstance()) {
String dateDeparture = TimeUtils.convertTimeToDepartureDate(timeMillis);
assertEquals("Date have wrong format or numbers!", "7 Oct 16:45", dateDeparture);
}
}


On our machines works fine. All tests passed. But on BB doesn't work.
This is log from BB console:

...
:app:testDebugUnitTest
com.stenaline.travelmate.dao.TimeUtilsTest > timeToDepartureTest FAILED
junit.framework.ComparisonFailure at TimeUtilsTest.java:46
com.stenaline.travelmate.dao.TimeUtilsTest > timeToMinutesAndHoursTest FAILED
junit.framework.ComparisonFailure at TimeUtilsTest.java:52
com.stenaline.travelmate.dao.TimeUtilsTest > timeToSpecificFormatTest FAILED
junit.framework.ComparisonFailure at TimeUtilsTest.java:70
com.stenaline.travelmate.dao.TimeUtilsTest > timeToOpeningHoursTest FAILED
junit.framework.ComparisonFailure at TimeUtilsTest.java:64
12 tests completed, 4 failed
...


I think BB works fine. We have a problem in our test. Can you some idea how to solve it?
Thanks you.

Answer

Problem was in timezone. BuddyBuild use PST and local machines / code use default EST or whatever. Solution is defined timezone for every time value.

for SimpleDateFormat:

 //...           
 SimpleDateFormat format = new SimpleDateFormat("d MMM HH:mm", Locale.getDefault());
    format.setTimeZone(TimeZone.getTimeZone("UTC"));
 //...

and for Calendar:

//...
Calendar cal = Calendar.getInstance();
cal.setTime(dateConverted);
cal.setTimeZone(TimeZone.getTimeZone("UTC"));
//...

And now test will be passed.

Comments