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()
.
Once created, a mock is in “recording” mode, meaning that EasyMock will record any action the Mock Object takes, and replay them in the “replay” mode. expect(…): with this method, we can set expectations, including calls, results, and exceptions, for associated recording actions.
Mockito supports both mocks as well as spies. Both spies and mocks perform different functions. Spy creates a partial mock object, whereas mock creates a dummy/ fake (fully mock) object of the real one. Whereas EasyMock supports only mocks.
If you still want to use EasyMock, because changing it doesn't depend on you (work in an enterprise) you can use reflection to change the private field which your method returns, or any private field for that matter. You can have these methods in a helper class for example. And use them as needed.
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).
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
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