0

I'm in JUnit, testing to see if file mocking works right. Something is going wrong and an exception is being thrown, yet it gives the reason as my "fail" call.

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

I uncommented line-by-line to see which line causes the failure. I want to debug with a real message but the output from my fail is this:

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)

How is it printing out the string related to calling fail, vs the IOException? I would expect to see something related to line 65.

Comments below explain why this was a duplicate.

Dave Stein
  • 8,653
  • 13
  • 56
  • 104
  • Because `catch (IOException e)` means you handled it! – SiKing Sep 14 '17 at 19:29
  • Why bother catching it at all? Just add `throws IOException` to the signature of `readFile()`, and let JUnit handle printing the exception. Your custom message adds no value over what is done by default. – Andy Turner Sep 14 '17 at 19:29
  • @AndyTurner: Not sure how this is duplicate because I am actually printing a string just fine. Also, I'm not calling a `readFile` method of my own right now. I was just verifying some vsf stuff before actually calling my class' methods. If I don't do a catch, I get a compilation error for not catching. @SiKing: I caught it, but I would think the trace is from `e`, so it would give the details. – Dave Stein Sep 14 '17 at 19:32
  • I edited my question as to why this isn't a duplicate. – Dave Stein Sep 14 '17 at 19:35
  • 2
    @DaveStein "I am actually getting a string printed fine" well, you're printing *a* string, for sure. But what you're actually printing is the result of printing a `StackTraceElement[]`; and since arrays don't override `toString()`, all you're getting is the class name and hash code, just the same as if you print `new int[0].toString()`. So, if you want a string related to the `IOException`, check the duplicate. – Andy Turner Sep 14 '17 at 19:36
  • @AndyTurner Ah. The way you explained it there makes sense to me. I have virtually no Java experience so I need things spelled out sometimes. – Dave Stein Sep 14 '17 at 19:37

0 Answers0