Getting EasyMock mock objects to throw Exceptions

后端 未结 2 1477

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

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-16 12:15

    From the documentation:

    For specifying exceptions (more exactly: Throwables) to be thrown, the object returned by expectLastCall() and expect(T value) provides the method andThrow(Throwable throwable). The method has to be called in record state after the call to the Mock Object for which it specifies the Throwable 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"));
    

提交回复
热议问题