我有一个声明、初始化的常量很少的类,并且我还有一个私有构造函数。由于某些原因,我正在编写 Junits 来实现代码覆盖率。在这里,我使用了 constructor.setaccessible(true) 并初始化了类。
在断言语句中,我期望构造函数的长度为 1。
我已经为这个类实现了 100% 的代码覆盖率。但我不太确定如何。任何人都可以对此有所了解吗?
public class CommonConstants {
public static final String ABC= "ABC";
public static final String XYZ= "XYZ";
private CommonConstants() {}
}
@Test
public void stringTest() {
final Constructor<?>[] constructors = CommonConstants.class.getDeclaredConstructors();
constructors[0].setAccessible(true);
try {
CommonConstants cc = (CommonConstants) constructors[0].newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
Assert.assertEquals(1, constructors.length);
}