What is the purpose of the andAnswer() method in EasyMock?
A) To define custom behavior for a mocked method
B) To provide multiple return values
C) To throw an exception from a mock method
D) To create a partial mock
Answer:
A) To define custom behavior for a mocked method
Explanation:
The andAnswer()
method in EasyMock is used to define custom behavior for a mocked method. It allows you to provide a custom implementation that will be executed when the method is called.
For example:
EasyMock.expect(mockService.someMethod()).andAnswer(() -> {
// Custom behavior here
return "Custom Value";
});
In this example, andAnswer()
is used to define custom behavior for someMethod()
that returns “Custom Value”.