Tests the contracts of comparable and equals:
Util classes should
public interface UtilClassConventionsTester {
Class<?> getClassUnderTest();
@Test
default void hasOnlyOnePrivateConstructor() {
Class<?> clazz = getClassUnderTest();
final Constructor<?>[] cons = clazz.getDeclaredConstructors();
assertEquals(1, cons.length, "Class " + clazz + "has more than one constuctor.");
assertTrue(Modifier.isPrivate(cons[0].getModifiers()), "Constuctor of class " + clazz + "is not private.");
}
@Test
default void isPublicClass() {
Class<?> clazz = getClassUnderTest();
assertTrue(Modifier.isPublic(clazz.getModifiers()), "Class " + clazz + " is not public.");
}
@Test
default void isFinalClass() {
Class<?> clazz = getClassUnderTest();
assertTrue(Modifier.isFinal(clazz.getModifiers()), "Class " + clazz + " is not final.");
}
}
Usage
// as Nested Test:
@Nested
class MyUtilsUtilClassConventionsTest implements UtilClassConventionsTester<MyUtils> {
public Class<?> getClassUnderTest() {
return MyUtils.class;
}
}