64

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().

Michu93
  • 5,058
  • 7
  • 47
  • 80
Surez
  • 854
  • 1
  • 8
  • 10
  • 4
    I think it is worth to mention that if you do not like calling replay() all then time you should try [Mockito](https://code.google.com/p/mockito/wiki/MockitoVSEasyMock) where it is not needed. – Michal Oct 07 '13 at 16:54
  • That's the most frustrating thing in this framework. I can't get it for about 4-5 months until I have to use it on daily basis. – Lidjan Dec 01 '17 at 08:56

4 Answers4

64

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).

Henri
  • 5,551
  • 1
  • 22
  • 29
  • 23
    Thanks for the response user753739. method name should have been named as 'play' or something like that instead or 'replay'. the word 'replay' is confusing in this context. – Surez May 15 '11 at 13:34
  • In what sort of scenario would I not want to call replay()? (For me the name 'activate' explains better what I expect it would do.) – Toast May 09 '16 at 11:29
  • Weird naming indeed for "expect", "replay" and "verify", but it is explained in my post. – CuongHuyTo Jun 27 '16 at 09:40
  • 1
    It's a record -> replay metaphore. Expect comes from setting expectations. Verify is verify that what you were expecting to be called was. – Henri Aug 08 '17 at 17:40
50

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

Kenny Worden
  • 4,335
  • 11
  • 35
  • 62
Anonymous
  • 501
  • 4
  • 3
18

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).
CuongHuyTo
  • 1,333
  • 13
  • 19
  • 1
    You are correct, I cant see a reason to call `replay` method at all. – vikingsteve Nov 28 '18 at 10:49
  • You need to remember that EasyMock was there before all other modern mocking frameworks. So it used the metaphor it wanted at that time. – Henri Aug 18 '21 at 11:34
1

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.

Dexter
  • 4,036
  • 3
  • 47
  • 55