我最近重构了我的一个类,以在其构造函数中接受可迭代的泛型对象,但现在无法让 JMockit 实例化测试类的 @Tested
字段。这是一个显示相同问题的精简测试用例:
import java.util.Collections;
import mockit.Injectable;
import mockit.Tested;
import org.junit.Test;
public class FooTest {
public static interface Generic<T> {}
public static class Foo<T> implements Generic<T> {
public Foo(Iterable<Generic<T>> iterable) {}
}
@Tested Foo<Object> tested;
@Injectable Iterable<Generic<Object>> injectable = Collections.emptyList();
@Test
public void testFoo() {
// java.lang.IllegalArgumentException: No constructor in class FooTest$Foo that can be satisfied by available injectables
}
}
我意识到我可以通过在 @Before
方法中创建 tested
来轻松解决这个问题,但我想首先了解为什么会失败。 :-)
我使用的是 Java 1.7.0_51、JMockit 1.8 和 JUnit 4.11。
最佳答案
从 JMockit 1.8 开始,@Tested
功能尚不完全支持泛型类型参数。
也就是说,JMockit 1.9(即将于 6 月 22 日发布)增加了对此类场景的支持。
关于java - 为什么 JMockit 不使用这个(显然)有效的 Injectable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24234332/