我在这段代码中看到,
expect(myService.getAll(anyBoolean())).andReturn(objectList).anyTimes();
replay(scopeService);
那个expect语句正在抛出
IllegalStateException - missing behavior definition for the preceding method call:
myService.getAll(true)
Usage is: expect(a.foo()).andXXX()
我知道,如果我没有 andReturn,或者我错过了调用重播,或者我的对象不是模拟,它会抛出此异常。我已经检查了所有这些,但事实并非如此!如果还有其他问题,有人可以告诉我吗? 我之前进行了大约 50 次预期/重播,没有出现任何问题。
最佳答案
您没有提供更多代码,因此我假设您的代码或多或少如下所示:
import static org.easymock.EasyMock.*;
public class SomeTests {
static class Foo {
public List<Integer> getAll(boolean value) {
throw new RuntimeException();
}
}
public void someTestCase() {
Foo mock = createMock(Foo.class);
List<Integer> ret = Collections.singletonList(1);
expect(mock.getAll(anyBoolean())).andStubReturn(ret);
replay(mock);
mock.getAll(true); // returns a list with single element 1 rather than throwing an exception
}
}
我的建议是:
- 检查
myService
实例是否是通过其中一种模拟方法创建的 - 在切换模拟模式时,应在
myService
上调用 Reply
关于java - Easymock.expect 抛出 IllegalStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61183656/