Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EasyMock void method

I'm trying to use EasyMock to mock out some database interface so I can test the business logic off a wrapping method. I've been going ok with methods that return by using the following in my setup of my test.

DBMapper dbmapper = EasyMock.createMock(DBMapper.class); userService.setDBMapper(dbmapper);         

then within my actual test I run

EasyMock.expect(dbmapper.getUser(userId1)).andReturn(mockUser1); EasyMock.replay(dbmapper); userService.getUser(userId1); 

This service then connects to the dbmapper and returns the object (the mapper is injected using setter methods)

These type of mocks seem to work fine. However when I try to run a test for

userService.addUser(newUser1); 

This method calls a void method.

dbmapper.createUser(newUser); 

It's this method that I'm having problems mocking out. I've tried the following

EasyMock.expectLastCall(); EasyMock.replay(dbMapper); userService.addUser(newUser1); 

as some other posts/questions etc seem to suggest I get an IlligalStateException: no last call on a mock available

Can anyone point me in the right direction please?

Many Thanks in advance

like image 200
FireEnigmaX Avatar asked Apr 03 '14 08:04

FireEnigmaX


People also ask

How do you expect void methods 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.

What method is used to make 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.

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.

How do you mock method which returns void?

Mockito provides following methods that can be used to mock void methods. doAnswer() : We can use this to perform some operations when a mocked object method is called that is returning void. doThrow() : We can use doThrow() when we want to stub a void method that throws exception.


2 Answers

This problem does not happens if you use the 'nice' API:

DBMapper dbmapper = EasyMock.createNiceMock(DBMapper.class); 

There are two kinds of mock - strict and nice. The strict mock throws Assertion Error in case an unexpected method is called. The nice mock allows unexpected method calls on the mock.

For further details, refer to the official doc - http://easymock.org/user-guide.html#mocking-strict

like image 33
RakeshK Avatar answered Oct 10 '22 22:10

RakeshK


You're close.

You just need to call the method on your mock before calling expectLastCall()

So you expectation would look like this:

userService.addUser(newUser1); EasyMock.expectLastCall(); EasyMock.replay(dbMapper); userService.addUser(newUser1); 

This works because the mock object is in Record mode before the call to replay(), so any calls to it will perform default behaviour (return null/do nothing) and will be eligible for replaying when the replay() method is called.

What I like to do to make sure that it is obvious the method call is for an expectation is to put a small comment in front of it like this:

/* expect */ userService.addUser(newUser1); EasyMock.expectLastCall(); EasyMock.replay(dbMapper); userService.addUser(newUser1); 
like image 165
Dan Temple Avatar answered Oct 10 '22 20:10

Dan Temple