Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock: Void Methods

I have a method that returns void in a class that is a dependency of the class I want to test.

This class is huge and I'm only using this single method from it. I need to replace the implementation of this method for the test as I want it to do something different and I need to be able to access the parameters this method receives.

I cannot find a way of doing this in EasyMock. I think I know how to do it with Mockito by using doAnswer but I don't want to add another library unless absolutely necessary.

like image 379
Iker Jimenez Avatar asked May 13 '09 16:05

Iker Jimenez


People also ask

How do you void in EasyMock?

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.

What does EasyMock expectLastCall do?

The API doc for EasyMock says this about expectLastCall() : Returns the expectation setter for the last expected invocation in the current thread. This method is used for expected invocations on void methods.

How does EasyMock work?

easymock. EasyMock: mock(…): generates a mock of the target class, be it a concrete class or an interface. Once created, a mock is in “recording” mode, meaning that EasyMock will record any action the Mock Object takes, and replay them in the “replay” mode.

What is EasyMock verify?

EasyMock can ensure whether a mock is being used or not. It is done using the verify() method. Take a look at the following code snippet. //activate the mock EasyMock. replay(calcService); //test the add functionality Assert.


1 Answers

If I understand what you want to do correctly, you should be able to use andAnswer():

mockObject.someMethod(eq(param1), eq(param2)); expectLastCall().andAnswer(new IAnswer() {     public Object answer() {         //supply your mock implementation here...         SomeClass arg1 = (SomeClass) getCurrentArguments()[0];         AnotherClass arg2 = (AnotherClass) getCurrentArguments()[1];         arg1.doSomething(blah);         //return the value to be returned by the method (null for void)         return null;     } }); 

The EasyMock User Guide explains:

Creating Return Values or Exceptions

Sometimes we would like our mock object to return a value or throw an exception that is created at the time of the actual call. Since EasyMock 2.2, the object returned by expectLastCall() and expect(T value) provides the method andAnswer(IAnswer answer) which allows [you] to specify an implementation of the interface IAnswer that is used to create the return value or exception.

Inside an IAnswer callback, the arguments passed to the mock call are available via EasyMock.getCurrentArguments(). If you use these, refactorings like reordering parameters may break your tests. You have been warned.

like image 129
matt b Avatar answered Oct 12 '22 01:10

matt b