Jacoco 显示行没有被覆盖,即使 IntelliJIdea 静态代码分析器说它们被覆盖

问题描述

环境:Java 8 / SpringBoot v2.0.2 / IntelliJIdea(终极版 - 2020.3.2)/

我有这个静态方法

public static boolean isAvailable(final Price price) {
if (price == null) {
    return false;
}
boolean isAvailable = true;
BigDecimal annualPremium = price.getAnnualPremium() != null ? price.getAnnualPremium() : BigDecimal.ZERO;
BigDecimal monthlyPremium = price.getMonthlyPremium() != null ? price.getMonthlyPremium() : BigDecimal.ZERO;
BigDecimal monthlyFirstMonth = price.getMonthlyFirstMonth() != null ? price.getMonthlyFirstMonth() : BigDecimal.ZERO;
if (BigDecimal.ZERO.compareto(annualPremium) == 0
        || BigDecimal.ZERO.compareto(monthlyPremium) == 0
        || BigDecimal.ZERO.compareto(monthlyFirstMonth) == 0) {
    isAvailable = false;
}
return isAvailable;

}

这是我的测试课, 注意: HomeContentsQuote 类有一个私有的认构造函数。因此,正在使用反射。曾尝试使用类名访问静态方法,但也没有奏效。

public class HomeContentsQuoteTest {
HomeContentsQuote homeContentsQuote;
@Mock
private HollardProviderProductValues hollardProviderProductValues;
@Mock
private HomeContentsQuoteTranslator homeContentsQuoteTranslator;
@Mock
private HomeContentsProduct product;
@Before
public void setup() throws Exception{
    initMocks(this);
    product = mock(HomeContentsProduct.class,new ReturnsMocks());
    Constructor<HomeContentsQuote> constructor = HomeContentsQuote.class.getDeclaredConstructor();
    constructor.setAccessible(true);
    homeContentsQuote = constructor.newInstance();
}
@Test
public void testNullPrice() {
    assertFalse(homeContentsQuote.isAvailable(null));
}
@Test
public void testNonZeroPremiums() {
    when(hollardProviderProductValues.getAnnualPremium()).thenReturn(Optional.of(new BigDecimal(548.06)));
    when(hollardProviderProductValues.getMonthlyPremium()).thenReturn(Optional.of(new BigDecimal(53.36)));
    when(hollardProviderProductValues.getAnnualisedMonthlyPremium()).thenReturn(Optional.of(new BigDecimal(53.36)));
    when(hollardProviderProductValues.getMonthlyFirstMonth()).thenReturn(Optional.of(new BigDecimal(53.36)));
    when(hollardProviderProductValues.getShowMonthlyTotal()).thenReturn(Optional.of(Boolean.TRUE));
    when(hollardProviderProductValues.getMonthlyAvailable()).thenReturn(Optional.of(Boolean.TRUE));
    when(hollardProviderProductValues.getAnnualAvailable()).thenReturn(Optional.of(Boolean.TRUE));
    homeContentsQuoteTranslator = new HomeContentsQuoteTranslator(hollardProviderProductValues,product);
    Price price = Price.create(homeContentsQuoteTranslator);
    assertNotNull(price);
    assertTrue(homeContentsQuote.isAvailable(price));
}
@Test
public void testZeroAnnualPremiums() {
    when(hollardProviderProductValues.getAnnualPremium()).thenReturn(Optional.of(new BigDecimal(0.00)));
    when(hollardProviderProductValues.getMonthlyPremium()).thenReturn(Optional.of(new BigDecimal(53.36)));
    when(hollardProviderProductValues.getAnnualisedMonthlyPremium()).thenReturn(Optional.of(new BigDecimal(53.36)));
    when(hollardProviderProductValues.getMonthlyFirstMonth()).thenReturn(Optional.of(new BigDecimal(53.36)));
    when(hollardProviderProductValues.getShowMonthlyTotal()).thenReturn(Optional.of(Boolean.TRUE));
    when(hollardProviderProductValues.getMonthlyAvailable()).thenReturn(Optional.of(Boolean.TRUE));
    when(hollardProviderProductValues.getAnnualAvailable()).thenReturn(Optional.of(Boolean.TRUE));
    homeContentsQuoteTranslator = new HomeContentsQuoteTranslator(hollardProviderProductValues,product);
    Price price = Price.create(homeContentsQuoteTranslator);
    assertNotNull(price);
    assertFalse(homeContentsQuote.isAvailable(price));
}

}

当我运行 mvn clean test 时,jacoco 说没有覆盖此方法的任何行。但是当我调试时,我可以清楚地看到测试代码到达了被测试方法的每一行。我使用 IntelliJIdea(终极版 - 2020.3.2)。当我使用覆盖率运行这些测试时,它会将方法标记为已覆盖(绿色)。

请帮我解决这个问题。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)