Ian
Ian

Reputation: 331

EasyMock: test that method in mock isn't called

As per title, just wondering if there is a mechanism with easymock to test if a method wasn't called during it's lifetime.

Upvotes: 33

Views: 27865

Answers (5)

Fabian Specht
Fabian Specht

Reputation: 1

you could use verify(mockObject)

Upvotes: -1

sharath
sharath

Reputation: 3616

This worked for me:

eastMockObject.method(arg); EasyMock.expectLastCall().andStubThrow(new AssertionError());

Upvotes: 0

Caleb Thornton
Caleb Thornton

Reputation: 534

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

Upvotes: 30

Wim Coenen
Wim Coenen

Reputation: 66733

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.

Upvotes: 18

Daniel Alexiuc
Daniel Alexiuc

Reputation: 13240

By default, Easymock will throw an exception for any methods that are called that you didn't explicitly set expectations for.

Upvotes: 13

Related Questions