【发布时间】:2014-07-29 20:27:41
【问题描述】:
如果我有:
public abstract class A {
private Object target; // <- **IMPORTANT**
protected Blah blah // ... Not directly relevant to the question,
protected Blah blah2 // they are just a potential reason to extend
// test class ...?
public int func (){
int hello = Integer.parseInt(target.Fields.want2Change);
return hello;
}
}
我想测试 A 类。 因为它是抽象的,所以我用 ATest 类来扩展它。 还有其他领域,但它们受到保护,这是合理的。
我的问题来自儿童班,我如何访问 parent 像 target 这样的私人字段。
我混合使用了模拟 (Mockito) 和反射。
我也尝试过只使用反射而不是扩展类。
【问题讨论】:
-
请参阅 stackoverflow.com/questions/7569444/… 和 stackoverflow.com/questions/34571/…,了解有关如何以及是否应对抽象类进行单元测试的讨论。
标签: java unit-testing reflection parent-child legacy