【发布时间】:2015-03-11 06:32:51
【问题描述】:
我们目前使用 Mockito + PowerMock 作为我们的主要模拟框架,一旦开始将我们的一些代码移动到 java 8,就会遇到一些问题。 因此,我们决定评估 jMockit 作为替代方案。我对模拟概念有很好的理解,但我承认我对 jMockit 的经验非常有限。
但是,我在测试一些在我看来应该是非常基本的东西时遇到了问题:被测类使用 new 在其构造函数中创建了某个其他类的实例。这个新的调用是我想要返回一个模拟实例的东西。
这是我正在使用的代码: 包 com.some.org.some.app;
import mockit.Expectations;
import mockit.Injectable;
import mockit.Mocked;
import org.testng.annotations.Test;
public class ClassUnderTestTest {
interface SomeService {
void doWork();
}
class ClassUnderTest {
private final SomeService service;
public ClassUnderTest() {
this.service = new SomeService() {
@Override
public void doWork() {
}
};
}
}
@Injectable
private SomeService mockService;
@Test
public void shouldBeAbleToMaskVariousNumbers() throws Exception {
new Expectations() {{
new ClassUnderTest();
result = mockService;
}};
}
}
当运行上面我得到以下异常:
java.lang.IllegalStateException: Missing invocation to mocked type at this point;
please make sure such invocations appear only after the declaration of a suitable
mock field or parameter
我使用 TestNG 作为测试框架,实际上我的测试方法有一堆参数,因为它期望数据提供者传递一些测试数据。
这是非常基本的,看起来我的方法不是 jMockit 方式。提前感谢您的支持。
【问题讨论】:
标签: java mocking testng jmockit