检查是否抛出例外并在抛出例外后继续
我想进行一个从文件中读取一些数据并将数据传递给函数的测试。该功能调用其他方法,其中一些方法引发了一些例外。我对如何查看使用文件中的参数调用该方法是否触发了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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的;尝试/捕获的声明将照顾好它。
替换此:
使用:
您可以自由进行一些记录,然后只需
assert.fail
,如果需要。essertthrows非常简单,只有这样做:
现在您知道它的工作原理,可以自己编写,从而添加,更改或日志或在此过程中在正确的位置进行的任何操作。但是,鉴于您知道这是
ioException
,而不是instanceof
检查您可以只是catch(ioexception e)
,更简单。Simple; a try/catch statement will take care of it.
Replace this:
With:
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:
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 aninstanceof
check you can justcatch (IOException e)
, simpler.