为什么我的IOException没有在JUnit中打印正确的stacktrace?

时间:2017-09-14 19:25:23

标签: java junit exception-handling

我在JUnit中,测试文件模拟是否正常。出现了一些问题并且抛出了异常,但它却给出了原因,因为我的失败"调用

@Test
public void readFile() {
  RegistryTask task = new RegistryTask(false, null);
  try {
    FileSystem fs = Jimfs.newFileSystem(Configuration.unix());
    Path foo = fs.getPath("/foo");
    Files.createDirectory(foo);
    String text = "hello world!";

    Path hello = foo.resolve("hello.txt"); // /foo/hello.txt
    Files.write(hello, ImmutableList.of(text), StandardCharsets.UTF_8);
    Path filepath = Paths.get("/foo/hello.txt");
    byte[] readBytes = Files.readAllBytes(filepath); // line 65
    // String readString = new String(readBytes);

    // assertEquals(text, readString);
  } catch (IOException e) {
    String err = e.getStackTrace().toString();
    fail("IOException:\n" + err); // line 71
  }
}

我逐行取消注释以查看哪一行导致失败。我想用真实的消息进行调试,但我失败的输出是:

readFile(RegistryTaskTest)  Time elapsed: 0.132 sec  <<< FAILURE!
java.lang.AssertionError: IOException:
[Ljava.lang.StackTraceElement;@363ee3a2
    at org.junit.Assert.fail(Assert.java:88)
    at RegistryTaskTest.readFile(RegistryTaskTest.java:71)

如何打印出与调用fail相关的字符串,与IOException相比?我希望看到与第65行相关的某些

下面的评论解释了为什么这是重复的。

0 个答案:

没有答案
相关问题