Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test the order of mocked calls using EasyMock

It's easy enough in EasyMock to do:

EasyMock.expect(service.methodCall());

but I noticed that this does not test the order in which I execute the calls, which in a case that I am trying to test is very important. Is there anyway to do this with EasyMock?

like image 446
lottolove Avatar asked Sep 18 '10 05:09

lottolove


People also ask

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.

How do you mock on EasyMock?

EasyMock also supports injecting mocks using annotations. To use them, we need to run our unit tests with EasyMockRunner so that it processes @Mock and @TestSubject annotations. Equivalent to mock(…), a mock will be injected into fields annotated with @Mock.

What is replayAll in EasyMock?

replayAll() – Registers all the created mocks in one batch. verifyAll() – Verifies all the mock operations in one batch. resetAll() – Resets all the mock operations in one batch.

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.


2 Answers

You can use the EasyMock.createStrictMock() for creating a mock thats capable of checking the order of method calls.

http://easymock.org/EasyMock3_0_Documentation.html

(search for "Checking Method Call Order Between Mocks" in the above link for examples).

like image 54
keshav84 Avatar answered Oct 20 '22 22:10

keshav84


If you need to test the order across different mocked objects, you can use EasyMock.createStrictControl() to create the mocks, run replay() & verify().

This site has some handy sample code: http://www.michaelminella.com/testing/mock-controls-with-easymock.html (archive.org mirror)

like image 32
Mark McDonald Avatar answered Oct 20 '22 22:10

Mark McDonald