我第一次使用Mockito,我想知道使用when(dao.create(order)).thenThrow(new SQLException());
和when(dao.create(order)).thenThrow(SQLException.class);
之间的区别是什么。两者似乎都运行得很好,我在Mockito API中找不到任何关于它的信息。
我能想到的唯一区别是,当使用new SQLException()
时,您可以在构造函数中输入参数。是这个吗?
when(dao.create(order)).thenThrow(new SQLException());
这将抛出您提供的异常对象。例如,您可以使用特定的参数创建异常,例如new SQLException("description of exception", "sql:code")
when(dao.create(order)).thenThrow(SQLException.class);
这将使用反射创建指定类的默认实例。
thenThrow可以接受throwable作为参数。你不应该监视throwable,因为这意味着逻辑也可以捕获错误,这通常是不可恢复的。
传递new SQLException()等价于SQLException.class