Suppress Log Messages During Unit Tests

Overview

This page describes how to suppress expected or harmless log messages during unit test execution. Suppressing these messages helps keep test output clear and focused on relevant information.

When to Use

Use log suppression when:

  • Unit tests trigger log messages that are expected and not actionable.
  • You want to prevent these messages from cluttering the console output.

Prerequisites

Add the following dependency to your project to use the provided test utilities:

<dependency> <groupId>org.onehippo.cms7</groupId> <artifactId>hippo-cms-testutils</artifactId> <scope>test</scope> </dependency>

Suppressing Log Messages with Log4jInterceptor

Use org.onehippo.testutils.log4j.Log4jInterceptor to suppress log messages generated by specific code sections during unit tests.

Implementation Steps

  1. Identify the code in your test that triggers the log messages you want to suppress. For example, calling componentManager.start() may log a warning.

    @Test public void myUnitTest() throws Exception { final SpringComponentManager componentManager = .... // ... componentManager.start(); // ... }
  2. Wrap the code that triggers the log messages with a Log4jInterceptor in a try-with-resources block. Specify the log level and the logger names to trap.

    @Test public void myUnitTest() throws Exception { final SpringComponentManager componentManager = .... // ... try (Log4jInterceptor interceptor = Log4jInterceptor.onError() .trap(loggerName1, loggerName2 /*, ... loggerNameN */).build()) { componentManager.start(); } // ... }
    • Replace onError() with onDebug(), onInfo(), onWarn(), or onFatal() to match the log level you want to suppress.
    • List the logger names as arguments to trap().

Verification

Run your unit test and verify that the specified log messages are not present in the console output.

Suppressing Log Messages with StringMatchFilter

If the code generating the log messages is deeply nested or not directly accessible in your test, use org.onehippo.testutils.log4j.StringMatchFilter. This filter suppresses log messages based on text matching.

Example Use Cases

  • Bootstrapping partial content into the repository during a test, which produces known warnings.
  • Partially mocking an object in a test, resulting in expected warnings.

Implementation Steps

Step 1: Configure StringMatchFilter in log4j2.xml

Add StringMatchFilter to the filters of your Console appender:

<Appenders> <Console name="console" target="SYSTEM_OUT"> <Filters> <ThresholdFilter level="error"/> <StringMatchFilter/> </Filters> <PatternLayout pattern="%d{dd.MM.yyyy HH:mm:ss} %-5p [%C.%M():%L] %m%n"/> </Console> </Appenders>

Step 2: Define Filter Values in log4j-filters.txt

In the same directory as log4j2.xml, create a file named log4j-filters.txt. List each filter value on a separate line. Any log message containing one of these values will be suppressed. Lines starting with # are treated as comments.

Example log4j-filters.txt:

#
# Copyright (C) 2020 BloomReach B.V.
#

Error initializing content for frontend-templatecomposer.xml in '/hippo:configuration/hippo:frontend/cms'
Error initializing content for frontend-loader.xml in '/hippo:configuration/hippo:frontend/cms/cms-static'
Error initializing content for frontend-cluster.xml in '/hippo:configuration/hippo:frontend/cms'

Verification

When running your tests, any log message containing one of the specified filter values will not appear in the console output. For example, the following message will be suppressed:

[Error initializing content for frontend-templatecomposer.xml in '/hippo:configuration/hippo:frontend/cms' because of unknown namespace 'hippo:frontend']

Share Feedback
Page: /build/development-tools/suppress-log-messages-during-unit-tests
Section: Build
Category *