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!
The expect() method tells EasyMock to simulate a method with certain arguments. The andReturn() method defines the return value of this method for the specified method parameters. The times() method defines how often the Mock object will be called. The replay() method is called to make the Mock object available.
andVoid() If we just want to mock void method and don't want to perform any logic, we can simply use expectLastCall(). andVoid() right after calling void method on mocked object. You can checkout complete project and more EasyMock examples from our GitHub Repository.
EasyMock can ensure whether a mock is being used or not. It is done using the verify() method.
The way EasyMock works is like this:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With