Expecting Log Output
Sometimes you will need to test for content of log output. For instance, you are testing some service deep inside the stack and do not have a direct access to it.
Configure a Log4j Logger
log4j.properties file (presuming a Maven file structure):
src +--test +--resources +--log4j.properties
with the following content:
This logger simply drops everything in STDIO.
Maven will place the
src/test/resources at the top of the classpath for tests but will not package into the final artifact, so your real Log4j configuration is safe.
Writing a spec
1 2 3 4 5 6 7 8 9
- Line 3: will start intercepting and accumulating all output to
- Line 5: get all log output as a single
- Line 6: splitting the log output into individual log lines
- Line 7: asserting the value in a specific line
- Line 8: restorin the STDIO back to its natural state for all other tests that follow.
The same technique can be done with
Do not forget to restore the STDIO or STDERR output, otherwise you will not see much of log output.
How to comment
The comment section below is to discuss documentation on this page.
If you have an issue, or discover bug, please follow instructions on the Support page