Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Easy way to mock chained method calls in Easymock

Tags:

easymock

Is there a simple way to mock this call:

objectA.getB().getC();

right now the way I do this is:

A mockA = EasyMock.createMock(A.class);
B mockB = EasyMock.createMock(B.class);
C mockC = EasyMock.createMock(C.class);

expect(mockA.getB()).andReturn(mockB);
expect(mockB.getC()).andReturn(mockC);

This is a bit of an overkill since all I care is to get mockC. Is there an easier way to do it?

like image 918
Quantum_Entanglement Avatar asked Nov 13 '22 03:11

Quantum_Entanglement


1 Answers

I know the question is about EasyMock, but i can't just sit on my hands and not tell you about Mockito. The mocking you would like to do, is fairly easy in Mockito.

A mockA = Mockito.mock(A.class, RETURNS_DEEP_STUBS);
C mockC = Mockito.mock(C.class);
Mockito.when(mockA.getB().getC()).thenReturn(mockC);

Note that Mockito started off as enhancement to EasyMock, you may read more about it here: https://code.google.com/p/mockito/wiki/MockitoVSEasyMock

like image 136
Lauri Avatar answered Dec 15 '22 01:12

Lauri