How do you throw an exception from a mock method in EasyMock?
A) andThrow()
B) expectThrow()
C) throwException()
D) withException()
Answer:
A) andThrow()
Explanation:
The andThrow()
method in EasyMock is used to specify that a mock method should throw an exception when called. This is useful for testing how the system under test handles exceptions thrown by its dependencies.
For example:
EasyMock.expect(mockService.someMethod()).andThrow(new RuntimeException("Error"));
In this example, andThrow()
is used to specify that when someMethod()
is called on mockService
, it will throw a RuntimeException
.