Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock: test that method in mock isn't called

Tags:

easymock

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

like image 552
Ian Avatar asked Dec 29 '09 01:12

Ian


People also ask

How do you expect a void method to call in EasyMock?

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.

Which of the given method is used to create EasyMock?

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.

Is EasyMock a mocking framework?

EasyMock is a mocking framework, JAVA-based library that is used for effective unit testing of JAVA applications. EasyMock is used to mock interfaces so that a dummy functionality can be added to a mock interface that can be used in unit testing.

What does EasyMock verify do?

EasyMock can ensure whether a mock is being used or not. It is done using the verify() method.


2 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

like image 74
Caleb Thornton Avatar answered Oct 15 '22 10:10

Caleb Thornton


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.

like image 32
Wim Coenen Avatar answered Oct 15 '22 09:10

Wim Coenen