【问题标题】:Test that should throw an exception when specified file doesn't exist当指定文件不存在时应该抛出异常的测试
【发布时间】:2013-06-27 00:29:32
【问题描述】:

您好,我遇到了这样的问题。我的类正在解析输入数据,当指定的文件不存在时它应该抛出异常。这样做的正确方法是什么?我做过这样的事情:

@Test
public void allOK() throws IOException {

    words = folder.newFile("words");

    String[] args = new String[10];
    args[0] = "-rw";
    args[1] = "20";
    args[2] = "-rf";
    args[3] = "4";
    args[4] = "-w";
    args[5] = "words";
    args[6] = "-r";
    args[7] = "results";
    args[8] = "-c";
    args[9] = "1000";

    Options options = Options.getInstance();

    options.setOptions(args);

    assertEquals(20, options.getRows());

}

当然文件夹是TemporaryFolder。我不认为我可以发送一个临时创建的文件字符串来运行。所以测试总是失败,因为找不到文件。那你能告诉我怎么做吗?

【问题讨论】:

    标签: java junit


    【解决方案1】:

    您绝对可以将临时文件发送到“函数”。我建议您需要发送整个文件路径而不仅仅是words,因为每次运行测试时,该文件都将位于一个新的临时文件夹中。赞args[5] = words.getAbsolutePath();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-08
      • 2011-04-27
      • 2014-04-18
      • 2017-09-05
      • 2021-02-08
      • 1970-01-01
      相关资源
      最近更新 更多