I\'m in process of using EasyMock to write Unit tests for a number of collaborating classes. One of these classes (lets call it Foo
) opens a network connection
From the documentation:
For specifying exceptions (more exactly:
Throwable
s) to be thrown, the object returned byexpectLastCall()
andexpect(T value)
provides the methodandThrow(Throwable throwable)
. The method has to be called in record state after the call to the Mock Object for which it specifies theThrowable
to be thrown.Unchecked exceptions (that is,
RuntimeException
,Error
and all their subclasses) can be thrown from every method. Checked exceptions can only be thrown from the methods that do actually throw them.
For example:
expectLastCall().andThrow(new HibernateException("Something terrible happened"));
expect(query.list()).andThrow(
new HibernateException("Something terrible happened"));
you can use the method andThrow(Throwable throwable)
in easy mock. Check the documentation - heading Working with Exceptions.
For example
expect(mock.voteForRemoval("Document"))
.andThrow(new RuntimeException(), 4);