Jacoco is not able to cover a class containing only static methods. I did not instantiate the class in the test class rather directly called the static method to test.
public class DateUtil {
final static String datePattern = "EEE MM/dd/yyyy";
public static String convertToGMTDate(Date date ) {
DateFormat df = new SimpleDateFormat(datePattern, Locale.getDefault());
df.setTimeZone(TimeZone.getTimeZone("GMT"));
return df.format(date);
}
}
class DateUtilTest {
static DateUtil dateutil;
@Test
void convertToGMTDate() {
Date date = new GregorianCalendar(2020, Calendar.FEBRUARY, 11).getTime();
String stringDate = dateutil.convertToGMTDate(date);
assertEquals("Tue 02/11/2020)",stringDate);
}
}
The error report highlighted just the class name "DateUtil" and reported that its 75% covered. What to do for the class to cover completely 100%?
Not instantiating the class in test method, decreases the coverage by 25% here. How does it makes sense? Is it flaw with JaCoCo?
Thanks in advance!
The "missing" coverage is indeed from the default constructor.
Since your DateUtil
class is just a Utility (or helper) class, add a private constructor to it, like so:
private DateUtil() {
// Utility class
}
(note that such classes are also usually declared final
)
JaCoCo should then expect it to be 100% covered.