9

I would like to understand EasyMock better, and working with it I came up with this question.

What I would like to do is setting up a negative expectation over an object, to check if a certain method is not called during the test (when verifying those initial expectations).

I know that the default behaviour of verify is checking both cases: your expectations were met, and no other calls were performed; but there is no record in the test that a certain method is not called, in other words, if you set an expectation over this method and it doesn't get called, your test will fail (confirming that your implementation is behaving properly!).

Is there a way using EasyMock to set this up? I couldn't find anything in the documentation.

Thank you for your attention, and in advance for your help!

1 Answers1

11

The way EasyMock works is like this:

  1. create a Mock Object for the interface you would like to simulate,
  2. record the expected behavior, and
  3. switch the Mock Object to replay state.

Like in following if you don't set any expectation:

mock = createMock(YourInterface.class); // 1
// 2 (we do not expect anything)
replay(mock); // 3

then it means that if ClassUnderTest call any of the interface's methods, the Mock Object will throw an AssertionError like this:

java.lang.AssertionError: 
  Unexpected method call yourMethodWhichYouDidNotExpectToBeCalled():

This itself is Negative Expectation checking.

Kuldeep Jain
  • 8,409
  • 8
  • 48
  • 73