如何检查模拟方法被调用了多少次?
How do I check how many times a mocked method was called?
我有一个在 @Before
方法中设置的模拟:
@Before
public void setup() {
mockery.checking(new Expectations() {
{
allowing(mockExecutor).schedule(with(capture(runnableRef, Runnable.class)), with(0L), with(TimeUnit.MILLISECONDS));
}
});
}
我已经尝试为这个测试用例添加一个特定的期望(这里只是检查 1):
mockery.checking(new Expectations() {
{
exactly(1).of(mockExecutor).schedule(with(capture(runnableRef, Runnable.class)), with(0L), with(TimeUnit.MILLISECONDS));
}
});
但是在结果中我可以看到它从来没有达到这个新的期望,因为它首先达到了 "allowed":
java.lang.AssertionError: not all expectations were satisfied
expectations:
allowed, already invoked 1 time: scheduledExecutorService.schedule(captured value <com.rbsfm.common.db.async.impl.ThrottledExecutor@eafc191>, <0L>,
expected once, never invoked: scheduledExecutorService.schedule(captured value <com.rbsfm.common.db.async.impl.ThrottledExecutor@eafc191>, <0L>,
在大多数单元测试中,我不关心 schedule
方法被调用的频率(并且它确实因测试而异),但是在一个测试中,我希望检查它是否已被调用恰好调用了 2 次。
有什么方法可以做到这一点,而不必为每个具有不同数字的测试重复模拟配置?
例如,我可以查询 Mockery
以查明该方法被调用的频率吗?
这里讨论了这种情况:http://www.jmock.org/override.html(感谢 Dick Hermann 将我指向该页面)。
基本思想是为测试设置一个状态机,只有当状态机使用 when
处于正确状态时才会激活期望。
public class ChildTest extends TestCase {
Mockery context = new Mockery();
States test = context.states("test");
Parent parent = context.mock(Parent.class);
// This is created in setUp
Child child;
@Override
public void setUp() {
context.checking(new Expectations() {{
ignoring (parent).addChild(child); when(test.isNot("fully-set-up"));
}});
// Creating the child adds it to the parent
child = new Child(parent);
test.become("fully-set-up");
}
public void testRemovesItselfFromOldParentWhenAssignedNewParent() {
Parent newParent = context.mock(Parent.class, "newParent");
context.checking(new Expectations() {{
oneOf (parent).removeChild(child);
oneOf (newParent).addChild(child);
}});
child.reparent(newParent);
}
}
我有一个在 @Before
方法中设置的模拟:
@Before
public void setup() {
mockery.checking(new Expectations() {
{
allowing(mockExecutor).schedule(with(capture(runnableRef, Runnable.class)), with(0L), with(TimeUnit.MILLISECONDS));
}
});
}
我已经尝试为这个测试用例添加一个特定的期望(这里只是检查 1):
mockery.checking(new Expectations() {
{
exactly(1).of(mockExecutor).schedule(with(capture(runnableRef, Runnable.class)), with(0L), with(TimeUnit.MILLISECONDS));
}
});
但是在结果中我可以看到它从来没有达到这个新的期望,因为它首先达到了 "allowed":
java.lang.AssertionError: not all expectations were satisfied
expectations:
allowed, already invoked 1 time: scheduledExecutorService.schedule(captured value <com.rbsfm.common.db.async.impl.ThrottledExecutor@eafc191>, <0L>,
expected once, never invoked: scheduledExecutorService.schedule(captured value <com.rbsfm.common.db.async.impl.ThrottledExecutor@eafc191>, <0L>,
在大多数单元测试中,我不关心 schedule
方法被调用的频率(并且它确实因测试而异),但是在一个测试中,我希望检查它是否已被调用恰好调用了 2 次。
有什么方法可以做到这一点,而不必为每个具有不同数字的测试重复模拟配置?
例如,我可以查询 Mockery
以查明该方法被调用的频率吗?
这里讨论了这种情况:http://www.jmock.org/override.html(感谢 Dick Hermann 将我指向该页面)。
基本思想是为测试设置一个状态机,只有当状态机使用 when
处于正确状态时才会激活期望。
public class ChildTest extends TestCase {
Mockery context = new Mockery();
States test = context.states("test");
Parent parent = context.mock(Parent.class);
// This is created in setUp
Child child;
@Override
public void setUp() {
context.checking(new Expectations() {{
ignoring (parent).addChild(child); when(test.isNot("fully-set-up"));
}});
// Creating the child adds it to the parent
child = new Child(parent);
test.become("fully-set-up");
}
public void testRemovesItselfFromOldParentWhenAssignedNewParent() {
Parent newParent = context.mock(Parent.class, "newParent");
context.checking(new Expectations() {{
oneOf (parent).removeChild(child);
oneOf (newParent).addChild(child);
}});
child.reparent(newParent);
}
}