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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
…where
serviceis a mockable field. ThehashCode()thing in the log statements is contrived, but the point is that your mock needs to respond to any number of calls togetName()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 togetName()and use a normalandReturn()for the call topostMessage(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.