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?
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.
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.
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.
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.
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).
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With