I have a mock that has been set up in an @Before
method:
@Before
public void setup() {
mockery.checking(new Expectations() {
{
allowing(mockExecutor).schedule(with(capture(runnableRef, Runnable.class)), with(0L), with(TimeUnit.MILLISECONDS));
}
});
}
I've tried adding a specific expectation to this test case (just checking against 1 here):
mockery.checking(new Expectations() {
{
exactly(1).of(mockExecutor).schedule(with(capture(runnableRef, Runnable.class)), with(0L), with(TimeUnit.MILLISECONDS));
}
});
However in the results I can see that it's never hitting this new expectation as it hits the "allowed" first:
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$1@eafc191>, <0L>,
expected once, never invoked: scheduledExecutorService.schedule(captured value <com.rbsfm.common.db.async.impl.ThrottledExecutor$1@eafc191>, <0L>,
In most of the unit tests I do not care how often the schedule
method has been called (and it does vary from test to test), however in one test I wish to check that it has been called exactly 2 times.
Is there any way to do this without having to repeat the mock configuration for each test with different numbers?
For example can I query the Mockery
to find out how often the method has been called?
This scenario is discussed here: http://www.jmock.org/override.html (Thanks to Dick Hermann who pointed me to the page).
The basic idea is that you set up a state machine for the test and only have the expectation active when the state machine is in the correct state using 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);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.