2016-08-01 43 views
0

我有一個在竹上隨機失敗的測試。這個比率就像每10個構建失敗一次。它從未在我的當地環境中失敗。測試隨着模擬服務和日期時間失敗隨機

這裏我的服務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); 
    } 
} 

在這裏,我隨機失敗的測試:

@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()); 
    } 
} 

任何幫助是非常讚賞。

回答

0

此問題是由於使用靜態字段forceEnable(由其他測試操縱)並行執行來自Bamboo的測試引起的。

0

它是如何完全失敗的?在這個例子中,我沒有看到ffxProperties或dateTimeService被注入,我可以證實這只是因爲您在發佈時簡化了這個問題嗎?

我還沒有看到問題,但通常情況下,當某些東西總是在我的盒子上成功,但在構建盒上定期失敗時,定時問題就在發揮作用,我確實看到了時代,所以我對此很懷疑。

但到目前爲止,我的第一個猜測是字段沒有被注入,或者以某種方式以某種方式返回當前時間 - 如果速度足夠快,那麼兩個調用可能返回相同的日期時間失敗?您可以通過設置斷點或記錄來確認日期時間是否是您認爲的那樣。

+0

斷言將失敗,tripAdvisorSwitchService.isEnabled()將返回true。 @InjectMocks將注入模擬。在我的本地dateTimeService.getCurrentDateTime()將始終返回新的DateTime(2016,9,14,14,1,0,0,utcTimezone)。 –