Ich habe einen Test, der zufällig auf Bamboo fehlschlägt. Das Verhältnis ist wie 1 Fehler alle 10 Builds. Es hat nie in meiner lokalen Umgebung versagt.Test mit verspotteten Service und DateTime fehlgeschlagen
hier mein Dienst semplified:
public final class TripAdvisorSwitchServiceImpl implements TripAdvisorSwitchService{
private FfxProperties ffxProperties = FfxProperties.getInstance();
private DateTimeFormatter dateTimeFormatter = DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ssZ");
@Reference
private DateTimeService dateTimeService;
private static boolean forceEnable;
@Override
public boolean isEnabled() {
if (forceEnable) {
return true;
}
String endDateStr = ffxProperties.get("tripadvisor.endDate");
DateTime endDate;
try {
endDate = dateTimeFormatter.parseDateTime(endDateStr);
} catch (IllegalArgumentException e) {
LOG.error("Date format is wrong: {}", endDateStr);
return true;
}
return dateTimeService.getCurrentDateTime().isBefore(endDate);
}
}
Und hier meinen zufälliges Versagen Test:
@RunWith(PowerMockRunner.class)
@PrepareForTest(FfxProperties.class)
public class TripAdvisorSwitchServiceImplTest {
private DateTimeZone utcTimezone = DateTimeZone.forTimeZone(TimeZone.getTimeZone("UTC"));
@InjectMocks
private TripAdvisorSwitchServiceImpl tripAdvisorSwitchService;
@Mock
FfxProperties ffxProperties;
@Mock
DateTimeService dateTimeService;
@Before
public void setup() throws IllegalAccessException {
MockitoAnnotations.initMocks(this);
mockStatic(FfxProperties.class);
when(FfxProperties.getInstance()).thenReturn(ffxProperties);
}
@Test
public void tripAdvisorShouldBeDisabledAfterTheEndDate() {
when(ffxProperties.get("tripadvisor.endDate")).thenReturn("2010-09-14T14:00:00+0000");
when(dateTimeService.getCurrentDateTime()).thenReturn(new DateTime(2016, 9, 14, 14, 1, 0, 0, utcTimezone));
assertFalse("It should be disabled", tripAdvisorSwitchService.isEnabled());
}
}
Jede Hilfe wird sehr geschätzt.
Die Assertion schlägt fehl und tripAdvisorSwitchService.isEnabled() gibt true zurück. @InjectMocks injiziert die Mocks. Auf meinem lokalen dateTimeService.getCurrentDateTime() wird immer neue DateTime (2016, 9, 14, 14, 1, 0, 0, utcTimezone) zurückgeben. –