【发布时间】:2019-01-19 04:31:17
【问题描述】:
我想知道如何测试引发 IllegalStateException 的私有构造函数,我已经搜索并发现了类似这样的内容:
@Test
public void privateConstructorTest()throws Exception{
Constructor<DetailRecord> constructor = DetailRecord.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}
这是构造函数:
private DetailRecord(){
throw new IllegalStateException(ExceptionCodes.FACTORY_CLASS.getMessage());
}
如果构造函数没有抛出异常,则测试有效
【问题讨论】:
-
似乎是 stackoverflow.com/questions/156503/… (Junit 4) 或 stackoverflow.com/questions/40268446/… (JUnit 5) 的副本。
标签: java testing junit constructor private