On test failure, dump logged but not yet asserted messages This greatly helps debugging failing tests as one now sees what messages are missing assertions. Change-Id: Ibaa3c822dfe1f22c51a6c7aef8630441b7c70668
diff --git a/src/test/java/com/googlesource/gerrit/plugins/its/base/testutil/LoggingMockingTestCase.java b/src/test/java/com/googlesource/gerrit/plugins/its/base/testutil/LoggingMockingTestCase.java index e7a137a..2d456c0 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/its/base/testutil/LoggingMockingTestCase.java +++ b/src/test/java/com/googlesource/gerrit/plugins/its/base/testutil/LoggingMockingTestCase.java
@@ -47,8 +47,7 @@ } } } - assertNotNull("Could not find log message containing '" + needle + "'", hit); - assertTrue("Could not remove log message containing '" + needle + "'", records.remove(hit)); + removeLogHit(hit, "containing '" + needle + "'"); } protected final void assertLogMessageContains(String needle) { @@ -66,10 +65,14 @@ hit = record; } } - assertNotNull("Could not find log message with a Throwable containing '" + needle + "'", hit); - assertTrue( - "Could not remove log message with a Throwable containing '" + needle + "'", - records.remove(hit)); + removeLogHit(hit, "with a Throwable containing '\" + needle + \"'"); + } + + private void removeLogHit(LogRecord hit, String description) { + if (hit == null) { + failWithUnassertedLogDump("Could not find log message " + description); + } + assertTrue("Could not remove log message " + description, records.remove(hit)); } // As the PowerMock runner does not pass through runTest, we inject log @@ -77,17 +80,27 @@ @After public final void assertNoUnassertedLogEvents() { if (records.size() > 0) { - LogRecord record = records.iterator().next(); - String msg = "Found untreated logged events. First one is:\n"; - msg += record.getMessage(); - Throwable t = record.getThrown(); - if (t != null) { - msg += "\n" + t; - } - fail(msg); + failWithUnassertedLogDump("Found unasserted logged events."); } } + public final void failWithUnassertedLogDump(String msg) { + msg += "\n"; + if (records.size() == 0) { + msg += "(All logged messages have already been asserted)"; + } else { + msg += records.size() + " logged, but not yet asserted messages remain:"; + for (LogRecord record : records) { + msg += "\n" + record.getMessage(); + Throwable t = record.getThrown(); + if (t != null) { + msg += "\n with thrown " + t; + } + } + } + fail(msg); + } + @Override public void setUp() throws Exception { super.setUp();