As per title, just wondering if there is a mechanism with easymock to test if a method wasn't called during it's lifetime.
5 Answers
I know this question is very old but I had the same question as the OP and did some more looking around. I found the following solution:
By adding .andThrow(new AssertionFailedError()).anyTimes();
at the end of your EasyMock declaration the test will fail when the mocked method is called.
The reason this is better than simply not using NiceMock and letting the test fail due to the unmocked method call is because this allows you to specifically test that XYZ method was not called in the given scenario.
I would like to give David Wallace credit for this answer. I found this solution in his answer on the following post: Test that void method didn't get called with EasyMock

- 1
- 1

- 524
- 4
- 8
-
2I believe that this should be the accepted answer. Reason being that unit tests are often changed with new requirements & it's very easy to miss out why a mock expectation wasn't set. This solution makes the missing method call explicit, thus demanding more attention from the person changing the code. – mindreader Jun 30 '16 at 19:36
-
you may want to replace `.andThrow(new AssertionFailedError()).anyTimes();` with a less verbose `andStubThrow(new AssertionFailedError());` – Dmitriusan Jan 11 '19 at 17:55
From the EasyMock documentation:
Nice Mocks
On a Mock Object returned by mock() the default behavior for all methods is to throw an AssertionError for all unexpected method calls. If you would like a "nice" Mock Object that by default allows all method calls and returns appropriate empty values (0, null or false), use niceMock() instead.
So what you are asking is the default behavior.

- 66,094
- 13
- 157
- 251
-
I find the default behavior annoying as you very easily end up "requiring" in the test that the code being tested is inefficient. I once did a simple refactor of moving a getSomething() call outside a loop, which caused the test to fail because i did not call getSomething 40 times(!), and "non-nice" mocks encourage this type of test (since it would fail when if I expected only one call before the refactoring). – Stein G. Strindhaug Jun 08 '11 at 09:32
-
1@Stein: agreed. Unit tests should be fine-grained, ideally testing only one thing. "non-nice" mocks discourage this. – Wim Coenen Jun 08 '11 at 10:04
-
2From my reading of the question, the OP wants a mock that will fail verification if it gets called. Nice mocks aren't what the OP wants because when a nice mock is called during testing it still passes verification. – Scott Shipp Jan 20 '17 at 18:36
By default, Easymock will throw an exception for any methods that are called that you didn't explicitly set expectations for.

- 13,078
- 9
- 58
- 73
This worked for me:
eastMockObject.method(arg); EasyMock.expectLastCall().andStubThrow(new AssertionError());

- 3,501
- 9
- 47
- 72
you could use verify(mockObject)

- 1
- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 29 '22 at 06:13