检查是否抛出例外并在抛出例外后继续

发布于 2025-01-21 11:03:27 字数 1152 浏览 2 评论 0原文

我想进行一个从文件中读取一些数据并将数据传递给函数的测试。该功能调用其他方法,其中一些方法引发了一些例外。我对如何查看使用文件中的参数调用该方法是否触发了ioexception的某个地方。我知道提供的代码段将停止执行,因为我使用了断言。如果要检查是否抛出了IOException,以及如果是的,则该如何写入错误消息,而无需停止执行测试?谢谢!

void test() throws IOException {
    Service service = helperFunction();
    File articles = new File("file.txt");
    Scanner scanner = new Scanner(articles);
    while(scanner.hasNextLine()) {
        String line = scanner.nextLine();
        line = line.replaceAll("[^\\d]", " ");
        line = line.trim();
        line = line.replaceAll(" +", " ");
        String[] numberOnTheLine = line.split(" ");

        List<Integer> list = Arrays.stream(numberOnTheLine).map(Integer::valueOf).collect(Collectors.toList());
        Article article = new Article(Long.valueOf(list.get(0)),
                new HashSet<>(List.of(new Version(list.get(1)))));

        List<List<Article>> listOfArticles = Collections.singletonList(List.of(article));
        Assertions.assertThrows(IOException.class,
                () -> service.etlArticles(listOfArticles.stream().flatMap(List::stream).collect(Collectors.toList())));
    }
}

I want to make a test that reads from a file some data and passes that data to a function. That function calls other methods and some of them throw some exceptions. I'm interested in how can I check whether or not calling the method with the parameters from the file triggered an IOException somewhere along. I know that the code snippet provided will stop the execution because I've used assert. How should I write if I want to check if an IOException was thrown and if it was, to get the error message, without stopping the execution of the test? Thanks!

void test() throws IOException {
    Service service = helperFunction();
    File articles = new File("file.txt");
    Scanner scanner = new Scanner(articles);
    while(scanner.hasNextLine()) {
        String line = scanner.nextLine();
        line = line.replaceAll("[^\\d]", " ");
        line = line.trim();
        line = line.replaceAll(" +", " ");
        String[] numberOnTheLine = line.split(" ");

        List<Integer> list = Arrays.stream(numberOnTheLine).map(Integer::valueOf).collect(Collectors.toList());
        Article article = new Article(Long.valueOf(list.get(0)),
                new HashSet<>(List.of(new Version(list.get(1)))));

        List<List<Article>> listOfArticles = Collections.singletonList(List.of(article));
        Assertions.assertThrows(IOException.class,
                () -> service.etlArticles(listOfArticles.stream().flatMap(List::stream).collect(Collectors.toList())));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自此以后,行同陌路 2025-01-28 11:03:27

简单的;尝试/捕获的声明将照顾好它。

替换此:

  service.etlartsicles(listofarticles.stream()。flatmap(list :: stream).collect(collectors.tolist()));
 

使用:

try {
 
 service.etlArticles(listOfArticles.stream().flatMap(List::stream).collect(Collectors.toList())));
} catch (IOException e) {
  // Code jumps to here if an IOException occurs during the execution of anything in the try block
}

您可以自由进行一些记录,然后只需assert.fail,如果需要。

essertthrows非常简单,只有这样做:

  try {
    runThatCode();
  } catch (Throwable e) {
    if (e instanceof TypeThatShouldBeThrown) {
      // Great, that means the code is working as designed, so, just...
      return;
    }
    // If we get here, an exception was thrown, but it wasn't the right type.
    // Let's just throw it, the test framework will register it as a fail.
    throw e;
  }

  // If we get here, the exception was NOT thrown, and that's bad, so..
  Assert.fail("Expected exception " + expected + " but didn't see it.");
}

现在您知道它的工作原理,可以自己编写,从而添加,更改或日志或在此过程中在正确的位置进行的任何操作。但是,鉴于您知道这是ioException,而不是instanceof检查您可以只是catch(ioexception e),更简单。

Simple; a try/catch statement will take care of it.

Replace this:

service.etlArticles(listOfArticles.stream().flatMap(List::stream).collect(Collectors.toList())));

With:

try {
 
 service.etlArticles(listOfArticles.stream().flatMap(List::stream).collect(Collectors.toList())));
} catch (IOException e) {
  // Code jumps to here if an IOException occurs during the execution of anything in the try block
}

You are free to e.g. do some logging and then just Assert.fail, if you want.

assertThrows is quite simple, all it does is this:

  try {
    runThatCode();
  } catch (Throwable e) {
    if (e instanceof TypeThatShouldBeThrown) {
      // Great, that means the code is working as designed, so, just...
      return;
    }
    // If we get here, an exception was thrown, but it wasn't the right type.
    // Let's just throw it, the test framework will register it as a fail.
    throw e;
  }

  // If we get here, the exception was NOT thrown, and that's bad, so..
  Assert.fail("Expected exception " + expected + " but didn't see it.");
}

Now that you know how it works, you can write it yourself and thus add or change or log or whatever you want to do during this process at the right place. However given you know it's IOException, instead of an instanceof check you can just catch (IOException e), simpler.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文