我有测试类继承问题:将@Mocked
和@Injectable
字段放到常见的抽象测试父类中会自动将模拟实例注入@Tested
类。
我使用 JMockit 1.5 。
这是一个例子:
public MyService {
private MyStorage storage;
public void myMethod(String entityId){
storage.getEntity(entityId);
// ...
}
}
public abstract class AbstractTestBase {
@Injectable
protected MyStorage storage;
}
public class MyTest extends AbstractTestBase {
@Tested
private MyService tested;
@Test
public void test_myMethod(){
new Expectations() {
{
storage.getEntity("1");
result = "foobar";
}
tested.myMethod("1"); // <-- here I have NPE
// as storage is not injected properly.
// ...
};
}
}
如果我将@Injectable
MyStorage存储移动到MyTest类,所有内容都会正确地注入@Tested
类。
如何允许对测试类使用公共父项进行自动注入?
答案 0 :(得分:1)
我发现问题已在最新版本1.11中解决。