Skip to content
Prev Previous commit
Next Next commit
Do not assume the length of the stacktrace doing test asserts
Different JDK are producing stacktraces of different length -> make the test dynamic and do not assume the length of the stacktrace is fixed and known up-front.
  • Loading branch information
brenuart committed Oct 11, 2022
commit 3d408bd27fdfc31a78a75b5953f6c3f28d299fb0
Original file line number Diff line number Diff line change
Expand Up @@ -192,15 +192,14 @@ public void testTruncateAfter() {
... 71 frames truncated
*/
assertThat(formatted)
.doesNotContain("org.junit")
.contains("generateSingle")
.endsWith("... 71 frames truncated" + converter.getLineSeparator());
.endsWith((e.getStackTrace().length - 9) + " frames truncated" + converter.getLineSeparator());

assertThat(countLines(formatted))
.isEqualTo(11);
}
}


@Test
public void testTruncateAfter_excluded() {
Expand Down Expand Up @@ -230,8 +229,12 @@ public void testTruncateAfter_excluded() {
... 71 frames truncated
*/
assertThat(formatted)
.contains("generateSingle")
.endsWith("... 71 frames truncated" + converter.getLineSeparator());
.containsSubsequence(
"four",
"... 3 frames excluded",
"generateSingle")
.endsWith(
"... " + (e.getStackTrace().length - 9) + " frames truncated" + converter.getLineSeparator());
}
}

Expand Down Expand Up @@ -260,7 +263,8 @@ public void testTruncateAfter_matchAll() {
*/
assertThat(countLines(formatted)).isEqualTo(3);
assertThat(formatted)
.endsWith("... 79 frames truncated" + converter.getLineSeparator());
.contains("eight")
.endsWith((e.getStackTrace().length - 1) + " frames truncated" + converter.getLineSeparator());
}
}

Expand Down Expand Up @@ -305,9 +309,11 @@ public void testTruncateAfter_commonFrames() {
*/
assertThat(formatted)
.containsSubsequence(
"66 frames truncated",
(e.getStackTrace().length - 8) + " frames truncated",
"Suppressed",
"75 frames truncated (including 73 common frames)");
"... " + (e.getSuppressed()[0].getStackTrace().length - 5) + " frames truncated (including ")
.endsWith(
"common frames)" + converter.getLineSeparator());
}
}

Expand All @@ -331,7 +337,8 @@ public void testExclusion_consecutive() {
String formatted = converter.convert(createEvent(e));
assertThat(formatted)
.containsSubsequence("3 frames excluded", "2 frames excluded");
assertThat(countLines(formatted)).isEqualTo(12);
assertThat(countLines(formatted))
.isEqualTo(12);
}
}

Expand All @@ -354,7 +361,8 @@ public void testExclusion_noConsecutive() {
assertThat(formatted)
.contains("one")
.doesNotContain("frames excluded");
assertThat(countLines(formatted)).isEqualTo(10);
assertThat(countLines(formatted))
.isEqualTo(10);
}
}

Expand Down Expand Up @@ -631,6 +639,10 @@ public void test_inline_hash_with_suppressed() {
}
}


// --------------------------------------------------------------------------------------------


private String extractClassAndMethod(String string) {
int atIndex = string.indexOf("at ");
int endIndex = string.indexOf('(');
Expand Down