在使用JUnit的JMockit测试中,我使用@Injectable注释来初始化通过Spring初始化的生产代码中的字段。
我在几个类中使用了一个类型的实现。为什么C不是作为IC的实现注入到A类型的模拟中,就像在下面提到的测试方法中的B和D类型中注入的那样?
@Service
@Scope("prototype")
public class A {
@Autowired IC c;
public IC getC() { return c; }
// do something using C in the method body
public void doSomething() {}
}
@Service
@Scope("prototype")
public class B {
@Autowired IC c;
public IC getC() { return c; }
// do something using C in the method body
public void doSomething() {}
}
public interface IC { void doSomething(); }
@Service
@Scope("prototype")
public class C implements IC {
@Override
public void doSomething() {}
}
@Service
public class D {
@Autowired IC c;
@Autowired B b;
@Autowired A a;
public IC getC() { return c; }
public B getB() { return b; }
public A getA() { return a; }
}
public class TestClass {
@Tested(fullyInitialized = true) D d;
@Injectable IC c;
@Tested @Injectable A a;
@Tested @Injectable B b;
@Test
public void test() {
// expectations are recorded here
assertNotNull(d.getC());
assertNotNull(d.getB().getC());
// Null reference
assertNotNull(d.getA().getC());
}
}编辑: @Tested和C的用例已经添加到代码示例中。这个注释被用来将C注入到A和B中,以便最终在它们的一些方法中使用它。
编辑2:是否允许对测试类中的多个字段使用@Tested来扩展所测试的单元?
发布于 2016-07-28 06:49:20
这个特性已经在jmockit 1.25中得到了修正。确保您使用的是最新版本。
https://stackoverflow.com/questions/29435856
复制相似问题