EasyMock allows us to mock exceptions when a specific method is called. We can do this using andThrow() method along with expect().

EasyMock Mock Exception Example

Let’s say we have a following class.

Here is the example of mocking StringUtils object and then stub its method to throw IllegalArgumentException.

We are using JUnit 5 Assertions to test exception and its message.

You can checkout complete project and more EasyMock examples from our GitHub Repository.

By admin

Leave a Reply

%d bloggers like this: