我是jUnit的新手。无法弄清楚如何测试处理的异常。如何使用jUnit测试已处理的异常?
public File inputProcessor(String filePath){
File file = null;
try {
file = new File(filePath);
Scanner input = new Scanner(file);
} catch (FileNotFoundException e) {
System.out.print("Check your input file path");
e.printStackTrace();
}
return file;
}
现在,要测试一个无效的文件路径来检查异常是否被抛出并正确捕获。我写了这个
@Test (expected = java.io.FileNotFoundException.class)
public void Input_CheckOnInvalidPath_ExceptionThrown() {
Driver driver = new Driver();
String filePath = "wrong path";
File file = driver.inputProcessor(filePath);
}
但是,因为我已经抓住了我的例外,它不工作。测试失败。任何帮助将是伟大的! thnx
如果即使引发异常,也不会引发异常,则会被正确捕获。有什么问题? –
你为什么要测试该方法不首先展示的行为!? – rocketboy
你有没有检查提出的解决方案[这里](http://stackoverflow.com/questions/17955899/test-handled-exceptions-junit)和[这里](http://stackoverflow.com/questions/12184281/how- to-handle-exceptions-while-unit-testing-using-junit) –