56

What is the difference between using andReturn(T value) vs andStubReturn(T value) for EasyMock?

In what situation would you use andStubReturn() where andReturn() can't achieve the same result?

WW.
  • 23,793
  • 13
  • 94
  • 121
Glide
  • 20,235
  • 26
  • 86
  • 135

2 Answers2

58

You use a stub return for a method call on the mock that you expect to happen but aren't otherwise interested in. You use a regular return for a "regular" method call.

Consider the following method:

public void someMethod(String arg) {
    if (logger.isDebugEnabled()) {
        logger.debug("Calling doSomething() on service " 
                       + service.getName().hashCode());
    }

    service.postMessage("{" + arg + "}");

    if (logger.isDebugEnabled()) {
        logger.info("Finished calling doSomething() on service " 
                      + service.getName().hashCode());
    }
}

...where service is a mockable field. The hashCode() thing in the log statements is contrived, but the point is that your mock needs to respond to any number of calls to getName() to avoid an NPE, while you couldn't otherwise care less about it.

When writing an EasyMock based unit test for this method, you'd andStubReturn() the call to getName() and use a normal andReturn() for the call to postMessage(String). When you verify the mock object, it'll only consider the latter and your the test doesn't break if you change the log4j config.

Barend
  • 17,296
  • 2
  • 61
  • 80
  • 11
    so let me see if I understand this correctly. Basically andStubReturn() is used for methods that we do not care about for our test for the mock objects , but we're required to mock the return otherwise, the code will not function. The andStubReturn() methods aren't verified by EasyMock; whereas andReturn() methods are verified. – Glide Sep 21 '10 at 07:50
  • Hi, will it be the same to use andReturn() with additional expectLastCall().anyTimes()? – Konstantin Milyutin Aug 27 '11 at 13:11
  • 1
    No it is not same. [Anytimes] enables the method to be called any number of times on the mock object, however all such calls will be verified. I.e. if any of the calls doesn't use mock object method then the result will fail. – SnoopyMe Mar 23 '12 at 03:17
  • 6
    @damluar: `andStubReturn()` sets a default return value which is used as a fallback only when regular `.andReturn()` have been used up. Examples by the EasyMock maintainer: http://tech.groups.yahoo.com/group/easymock/message/1069 – Stefan L Jul 20 '12 at 13:27
6

An additional note for clarity.

If you use .andStubReturn() (or if you use .andReturn(foo).anyTimes()), there will be no minimum expected call count. So if you set a mock expectation using either of these two, and the mocked method is NOT called, the .verify() call will not assert.

Example that will NOT assert when the mocked method isn't called;

FooClass myFooClass = EasyMock.createNiceMock(FooClass.class);
EasyMock.expect(myFooClass.someMethod(EasyMock.anyInt()).andStubReturn(true);
EasyMock.replay(myFooClass);

EasyMock.verify(myFooClass);

Example that WILL assert when the mocked method isn't called;

FooClass myFooClass = EasyMock.createNiceMock(FooClass.class);
EasyMock.expect(myFooClass.someMethod(EasyMock.anyInt()).andReturn(true).atLeastOnce();
EasyMock.replay(myFooClass);

EasyMock.verify(myFooClass);
Ben
  • 313
  • 4
  • 8