Saturday, 7 May 2016

java - Why is my IOException not printing correct stacktrace in JUnit?

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.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...