如何为私有构造函数编写@test类.我想用emma工具覆盖它.
public final class Product {
private Product() {
}
}
Run Code Online (Sandbox Code Playgroud)
有人可以建议一个简单的方法?
谢谢.
测试私有方法的最佳方法是使用Reflection.
有很多方法,但我会这么做;
@Test
public void testConstructorIsPrivate() throws Exception {
Constructor constructor = Product.class.getDeclaredConstructor();
assertTrue(Modifier.isPrivate(constructor.getModifiers()));
constructor.setAccessible(true);
constructor.newInstance();
}
Run Code Online (Sandbox Code Playgroud)
这将覆盖运行coverage工具emma时的构造函数.
| 归档时间: |
|
| 查看次数: |
3929 次 |
| 最近记录: |