如何使用jUnit测试已处理的异常? [英] How to test already handled exception using jUnit?

查看:216
本文介绍了如何使用jUnit测试已处理的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是jUnit的新手。无法确定如何测试处理的异常。

  public File inputProcessor(String filePath){
文件文件= null ;
try {
file = new File(filePath);
扫描仪输入=新的扫描仪(文件);
} catch(FileNotFoundException e){
System.out.print(检查输入文件路径);
e.printStackTrace();
}
返回文件;
}

现在,要使用无效的文件路径测试是否抛出异常并抓住了。我写了这个

  @Test(expected = java.io.FileNotFoundException.class)
public void Input_CheckOnInvalidPath_ExceptionThrown()
Driver driver = new Driver();
String filePath =错误路径;
文件文件= driver.inputProcessor(filePath);
}

但是,因为我已经抓住了我的异常,它不工作。测试失败。任何帮助将是伟大的! thnx

解决方案

您需要测试方法的行为,而不是其实现细节。



如果您的方法的正确行为是在文件不存在时返回 null ,则只需要

  @Test 
public void Input_CheckOnInvalidPath_ExceptionThrown(){
Driver driver = new Driver();
String filePath =错误路径;
assertNull(driver.inputProcessor(filePath));
}

如果您的方法的正确行为是将特定消息打印到 System.out 当文件不存在,并且您想要测试时,您可以创建一个模拟 PrintStream ,使用 System.setOut(PrintStream)设置它,调用您的方法,然后测试 PrintStream 被正确调用。 Mockito可以帮助你做到这一点 - 也许。我认为你有风险测试执行细节, System.out.println() vs许多 System.out.print()被调用。 (你可能不应该测试如何执行 e.printStackTrace()。)



如果正确的行为是两个,那么你需要两个支票。


I am new to jUnit. Can not figure out how to test handled exception.

 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;
}

Now, want to test with a invalid file path to check whether exception is thrown and caught properly. I wrote this

@Test (expected = java.io.FileNotFoundException.class)
public void Input_CheckOnInvalidPath_ExceptionThrown() {
    Driver driver = new Driver();
    String filePath = "wrong path";
    File file = driver.inputProcessor(filePath);
      }

But as I have already caught my exception its not working. Test is failing . Any help will be great!! thnx

解决方案

You need to test your method's behavior, not its in implementation details.

If the correct behavior of your method is to return null when the file does not exist, you just need

@Test
public void Input_CheckOnInvalidPath_ExceptionThrown() {
    Driver driver = new Driver();
    String filePath = "wrong path";
    assertNull(driver.inputProcessor(filePath));
  }

If the correct behavior of your method is to print a particular message to System.out when the file does not exist, and you want to test that, then you can create a mock PrintStream, use System.setOut(PrintStream) to set it, call your method, and then test that the PrintStream was invoked correctly. Mockito can help you do that -- maybe. I think you run the risk of testing the implementation detail that System.out.println() vs many System.out.print() is invoked. (You probably shouldn't be testing how e.printStackTrace() is implemented.)

If the correct behavior is both, then you need both checks.

这篇关于如何使用jUnit测试已处理的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆