Tip #18: (Spring) Use CapturedOutput Extension to test log output.
I had this requirement once, the system should log this and that…
How can I test logs?
If you are a Spring user, you can use CapturedOutput.
@Test
@ExtendWith(OutputCaptureExtension.class)
void testLogOutput(CapturedOutput output) {
//make your normal call here
//now you can test output here
assertThat(output.getOut()).contains(“my expected log”);
}
I’m not a Spring user, how can I do it?
Well, you can add handlers/appenders on your Logger to test it.
Have you ever needed to test your output? Let me know your use case!
Happy coding!