Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Lombok's @UtilityClass auto-generated code

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?

like image 921
Elizabeth Sallow Avatar asked Oct 15 '25 18:10

Elizabeth Sallow


1 Answers

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();
    });
  }
like image 98
Kevin D Avatar answered Oct 18 '25 10:10

Kevin D