Saturday, April 30, 2016

EasyMock Cheat-sheet

Here a Cheat sheet for EasyMock testing Framework. Because of tricky behaviour and hard to remind stuff in mocking science, this sheet helps to get back on track when you have to test.

Mock Types

Nice Mock

EasyMock.createNiceMock(Class<T> toMock)
Creates a mock of a class (or implements an interface). Order of methods calls are not verified. A nicemock returns 0, null or false for all unexpected calls.

Mock

EasyMock.createMock(Class<T> toMock)
Creates a mock of a class (or implements an interface). Order of methods calls are not verified. An unexpected method call to the mock raises an error.

Strict Mock

EasyMock.createStrictMock(Class<T> toMock)
Creates a mock of a class (or implements an interface). An unexpected method call to the mock raises an error. Also order of methods call is checked and matters.

Expect

Describes an expected behaviour on a Mock

Standard Case

EasyMock.expect(mock.voteForRemoval("Document"))
.andReturn((byte) 42).times(3)

Void method

mockedService.addUser(newUser1);
EasyMock.expectLastCall().once();

AndReturn vs andStubReturn

AndStubReturn is a way to specify a default behaviour. If a method call is not recorded ( by an expect/return), the mock will return what is defined by stubReturn (expect/andStubReturn).
List list = createStrictMock(List.class );
expect(list.get(1)).andStubReturn("a");
expect(list.get(1)).andReturn("b");
expect(list.get(1)).andReturn("c");
replay(list);
assertEquals("b", list.get(1));
assertEquals("c", list.get(1));
assertEquals("a", list.get(1));
assertEquals("a", list.get(1));
verify(list);
Logically,  andStubReturn is a void method ; we cannot specify anyTimes nor once, because it is mock's default behaviour. An expect/andStubreturn can be verified 0 or any times.

What's the point between andReturn(foo).anyTimes() vs. andStubReturn(foo)  ?


EasyMock starts to verify all methods calls recorded by expect/andReturn, then it uses if needed default behaviour recorded by expect/andStubReturn (cf example above : b, then c, then a are returned).

AndAnswer, AndStubAnswer

EasyMock.expect(blah.getStuff()).      
andStubAnswer(new IAnswer<List<String>>() {
      @Override
      public List<Object> answer() throws Throwable {
           return new ArrayList<String>();
      }
}).anyTimes();
Instead of
EasyMock.expect(blah.getStuff().andReturn(new ArrayList<String>()))
.anyTimes()

AndAnswer, AndStubAnswer are ways to create new objects instances at each method call. Otherwise same object instance is returned. A useful trick to avoid side effects.

But why verify ?

EasyMock will not report uncalled methods until you call EasyMock.verify() at the end of your tests. It will report unexpected methods, but not the absence of expected methods.

Annotations

Runner

@RunWith(EasyMockRunner.class) 
Annotation for test class

Class under test

@TestSubject 
private ClassUnderTest classUnderTest = new ClassUnderTest();

A mock

@Mock 
private Collaborator mock;

Tip for runner

If we cannot modify runner of test class, annotation processing by easymock can be activated by using a rule :
@Rule
public EasyMockRule mocks = new EasyMockRule(this);

Good practice

-always call replay() on all mocks, always once
-always call verify() on all mocks, always once

Capture

A way to verify a parameter's value passed to a mock :
Capture<Joke> capturedJoke =  EasyMock.newCapture();

jokeDao.persistJoke(EasyMock.capture(capturedJoke));
EasyMock.expectLastCall().once();
//...replay...
//...verify...
Assert.assertEquals(jokeToStore, capturedJoke.getValue().getJokeContent());

Source files

Please find few examples in this Github : https://github.com/GermainSIGETY/easyMockCheatSheet