How can I get full coverage on class of static utility methods?
Consider the following minimal class:
public abstract class UtilityClass {
private UtilityClass() {}
public static void utilityMethod() {}
}
When I run the tests with coverage, the empty constructor always comes up as not covered:
- it doesn't matter if I mark the class abstract
- it doesn't matter if I mark the constructor private or not define it at all
Is there a way to get IDEA report 100% coverage of this class (other than the silly solution of testing the empty constructor)?
请先登录再写评论。
please vote for https://youtrack.jetbrains.com/issue/IDEA-26988.
Strictly speaking, your class is never initialized -> you can't reach 100% coverage.
Thanks,
Anna
Well, I should be able to get 100% coverage of the code that can actually get called. The empty constructor in the bytecode is dead code. Come on, it's called IntelliJ for a reason :-)
Here's a solution I found at https://bhupenderhbti.blogspot.com/2017/07/test-for-private-constructor-to-get.html:
@Test
public void testPrivateConstructor() throws Exception {
final Constructor constructor = Helper.class.getDeclaredConstructor();
assertTrue("Constructor is not private", Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}