I have a class that's annotated with @UtilityClass
like this
@UtilityClass
public class myUtilClass {
...
}
JaCoCo doesn't give me full coverage for this class because of the auto-generated code created by the annotation @UtilityClass
.
Ideally, I do not want to change any config files to ignore auto-gen code. How can this code be tested?
Just write this unit test to ensure your class cannot be instantiated because of @UtilityClass annotation. Then, coverage will be OK.
@Test
void test_cannot_instantiate() {
assertThrows(InvocationTargetException.class, () -> {
var constructor = YOUR_CLASS_NAME.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
});
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With