Asserting log entries in JUnit Jupiter is not as straight forward as one might want, but after some digging I found a way that worked for me. It should also me said that doing this i Jupiter is far easier than in previous versions of JUnit
The trick here is to use the SpringBoot OutputCaptureExtension on you test class. This can be accomplished with the @ExtendWith annotation in SpringBoot. This will inject a CapturedOutput object with output from our application
Example test class:
... import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.OutputCaptureExtension; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.CoreMatchers.containsString; @Test @ExtendWith(OutputCaptureExtension.class) public void testHappyCaseSendDataToSystem(CapturedOutput output) throws Exception { MockEndpoint mock = camelContext.getEndpoint("mock:senddataToSystem", MockEndpoint.class); // Setting expectations mock.expectedMessageCount(1); // Invoking consumer producerTemplate.sendBody("direct:start", null); // Asserting mock is satisfied mock.assertIsSatisfied(); // Assert log message assertThat(output.getOut(), containsString("LogMessage=Completed")); }
Tested on SpringBoot v3.2.0 and JUnit 5.10.1
0 Comments.