What is EasyMock.replay() used for?
TestingJunitEasymockTesting Problem Overview
I'm a newbie to unit testing and Junit. I know the basics of Junit. I just started learning about the EasyMock framework.
I couldn't understand the use of replay()
method.
Could anyone please provide some info?
I understand the use of EasyMock.expect()
and EasyMock.verify()
.
Testing Solutions
Solution 1 - Testing
The replay method is used to pass the mock from recording (where you record the method you expect to be called) to replaying state (where you actually test).
Solution 2 - Testing
You can remember it like this: When you write EasyMock.expect(abc.someMethod).andReturn(answer)
, you recorded the expected behaviour. But, when you write EasyMock.replay(abc)
, you are actually activating it.
I found this example very useful: http://www.tutorialspoint.com/easymock/easymock_adding_behavior.htm
Solution 3 - Testing
With EasyMock, when you "expect" you are actually record the desired fake/mocked behavior. So when you want to inject this mocked behavior onto a test runner (e.g. JUnit) you would "replay" your records.
Weird name comparing to other mocking framework indeed, a better name should be
- expect --> register
- replay --> activate (or no need to call this at all).
Solution 4 - Testing
As mentioned in above posts, the name is misleading. EasyMock.replay() activates the expectation. It is also desired that the expectation should be activated by default after we define one. Many other framework like Mockito does the same.
However I see one point (I may be wrong), that the designer might have thought is, scenario like below:
TestClass testObj = EasyMock.createMock(TestClass.class);
expect(testObj.testMethod(testInputOne).andReturn(testOutputOne);
expect(testObj.testMethod(testInputTwo).andReturn(testOutputTwo);
expect(testObj.testMethod(testInputThree).andReturn(testOutputThree);
//...
EasyMock.replay(testObj);
Here we are setting different expectations for different inputs to the same method. Then, activating all expectations in one go. Framework is not activating an expectation each time we define it.