有返回值的函数方法
有如下方法
public String testMethod1(String str) {
//业务逻辑
return name;
}
有返回值的函数方法在mock抛出异常时需要使用thenThrow方法,如下所示
PowerMockito.when(studentDAO.testMethod1(ArgumentMatchers.any())).thenThrow(new Exception("exception"));
无返回值的函数方法
有如下方法
public void testMethod2(String str) {
//业务逻辑
return;
}
对于无返回值的函数方法,即void方法,在mock方法抛出异常时,应使用doThrow方法,如下所示
PowerMockito.doThrow(new Exception("Something went wrong")).when(student).testMethod2();