使用 mockito 时在 catch 块中覆盖 EXCEPTION
Cover EXCEPTION in the catch block while using mockito
待测函数:
public void deleteFile(File file) {
try {
Files.delete(file.toPath()) ;
log.info("Csv file deleted from system succesfully after processing the data");
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
测试代码:
@Test
void testExceptionDuringDeletingFile() {
helper=mock(Helper.class);
//String bucket ="bucket";
//String key = "key";
File file = new File("");
doThrow(IOException.class).when(helper).deleteFile(file);
Assertions.assertThrows(IOException.class,()->helper.deleteFile(file));
}
但我收到以下异常:
org.mockito.exceptions.base.MockitoException: Checked exception is
invalid for this method! Invalid: java.io.IOException at
com.lululemon.product.ap.reprocess.producer.HelperTests.testExceptionDuringDeletingFile(HelperTests.java:191)
at
java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native
Method) at
因为您的 deleteFile()
方法不会抛出 IOException
。你 catch
它在你的方法实现中并用它做一些事情。
而且,你的模拟 Helper
class 实例真的不能从 deleteFile()
方法中抛出 IOException
因为真正的 deleteFile()
方法不会.
从您的方法中抛出 IOException
,您将不会收到错误。
删除文件方法:
public void deleteFile(File file) throws IOException {
try {
Files.delete(file.toPath());
log.info("Csv file deleted from system successfully after processing the data");
} catch (IOException e) {
throw new IOException("Couldn't delete file. Try again!");
}
}
测试方法:
@Test
void testExceptionDuringDeletingFile() throws IOException {
File file = new File("");
Helper helper = mock(Helper.class);
doThrow(IOException.class).when(helper).deleteFile(file);
Assertions.assertThrows(IOException.class, () -> helper.deleteFile(file));
}
待测函数:
public void deleteFile(File file) {
try {
Files.delete(file.toPath()) ;
log.info("Csv file deleted from system succesfully after processing the data");
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
测试代码:
@Test
void testExceptionDuringDeletingFile() {
helper=mock(Helper.class);
//String bucket ="bucket";
//String key = "key";
File file = new File("");
doThrow(IOException.class).when(helper).deleteFile(file);
Assertions.assertThrows(IOException.class,()->helper.deleteFile(file));
}
但我收到以下异常:
org.mockito.exceptions.base.MockitoException: Checked exception is invalid for this method! Invalid: java.io.IOException at com.lululemon.product.ap.reprocess.producer.HelperTests.testExceptionDuringDeletingFile(HelperTests.java:191) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
因为您的 deleteFile()
方法不会抛出 IOException
。你 catch
它在你的方法实现中并用它做一些事情。
而且,你的模拟 Helper
class 实例真的不能从 deleteFile()
方法中抛出 IOException
因为真正的 deleteFile()
方法不会.
从您的方法中抛出 IOException
,您将不会收到错误。
删除文件方法:
public void deleteFile(File file) throws IOException {
try {
Files.delete(file.toPath());
log.info("Csv file deleted from system successfully after processing the data");
} catch (IOException e) {
throw new IOException("Couldn't delete file. Try again!");
}
}
测试方法:
@Test
void testExceptionDuringDeletingFile() throws IOException {
File file = new File("");
Helper helper = mock(Helper.class);
doThrow(IOException.class).when(helper).deleteFile(file);
Assertions.assertThrows(IOException.class, () -> helper.deleteFile(file));
}