How do you mock a method to return different values on consecutive calls in EasyMock?
A) andReturn().andReturn()
B) expectReturn()
C) thenReturn().thenReturn()
D) withValues()
Answer:
A) andReturn().andReturn()
Explanation:
In EasyMock, you can mock a method to return different values on consecutive calls using multiple andReturn()
methods chained together. This allows you to specify a sequence of return values for the same method call.
For example:
EasyMock.expect(mockService.someMethod())
.andReturn("First Value")
.andReturn("Second Value");
In this example, the first call to someMethod()
will return “First Value”, and the second call will return “Second Value”.