2016-08-01 49 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)。 –