Environment: Java 8 / SpringBoot v2.0.2 / IntelliJIdea (Ultimate Edition - 2020.3.2) /
I have this static method
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;
}
Here is my test class, NOTE: HomeContentsQuote class has a private default constructor. Hence, reflections being used.Have tried accessing the static method using the class name but, that too didn't work.
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));
}
}
When I run mvn clean test
jacoco says non of this method's lines are covered. But when I debug I can see clearly that the test code reaches every line of the method being tested. I use IntelliJIdea (Ultimate Edition - 2020.3.2). When I run these tests with coverage it marks that method lines as covered(green).
Please help me figuring this out.