eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

eBook – Guide Junit – NPI (tag = JUnit)
announcement - icon

Improve your tests with JUnit 5, from mastering the basics to employing the new powerful features from JUnit 5 like extensions, tagging, filtering, parameterized tests, and more:

>> The Junit 5 handbook

1. Overview

When developing Java applications, we use logging for debugging and monitoring. Capturing and validating these logs is an important part of ensuring our application functions as expected.

In this tutorial, we’ll explore how to assert log messages in our JUnit tests. We’ll cover two approaches: asserting logs in native Java tests and in Spring Boot tests.

2. Setting up Test Environment

Before asserting our log statements, we’ll need to include a few dependencies and configure our application correctly.

2.1. Dependencies

Let’s start by adding the logging dependency to our project’s pom.xml file:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.5.16</version>
</dependency>

We’re using SLF4J as the logging facade and Logback as its underlying implementation.

Next, to write assertions in our tests, we’ll include the assertj-core dependency:

<dependency>
    <groupId>org.assertj</groupId>
    <artifactId>assertj-core</artifactId>
    <version>3.27.2</version>
    <scope>test</scope>
</dependency>

AssertJ provides a fluent and expressive API for writing assertions in our tests.

Finally, If we’re following the Spring Boot approach for asserting logs, we’ll also need to include the spring-boot-test dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-test</artifactId>
    <version>3.4.1</version>
    <scope>test</scope>
</dependency>

This dependency provides the necessary testing classes for Spring Boot applications, including the OutputCaptureExtension which we’ll use later in the tutorial.

2.2. Logback Configuration

Next, to generate logs, we’ll create a logback.xml file in our src/test/resources folder:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <root level="error">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>

We keep the logging configuration as simple as possible and redirect all logs to a CONSOLE appender using the specified pattern layout.

2.3. A Basic Business Function

Now, let’s create a class that will generate logs we can base our tests on:

class BusinessWorker {
    private static Logger LOGGER = LoggerFactory.getLogger(BusinessWorker.class);

    public void generateLogs(String msg) {
        LOGGER.trace(msg);
        LOGGER.debug(msg);
        LOGGER.info(msg);
        LOGGER.warn(msg);
        LOGGER.error(msg);
    }
}

Our BusinessWorker class exposes a single generateLogs() method which generates a log with the same content for each log level.

Although this method isn’t that useful in the real world, it’ll serve well for our testing purposes.

3. Asserting Logs in Native Java

Now that we have our test environment set up, let’s explore how to assert log messages in native Java applications.

3.1. Creating a MemoryAppender

First, let’s create a custom appender that keeps logs in memory. We’ll extend the ListAppender<ILoggingEvent> that logback offers, and we’ll enrich it with a few useful methods:

class MemoryAppender extends ListAppender<ILoggingEvent> {
    public void reset() {
        this.list.clear();
    }

    public boolean contains(String string, Level level) {
        return this.list.stream()
          .anyMatch(event -> event.toString().contains(string)
            && event.getLevel().equals(level));
    }

    public int countEventsForLogger(String loggerName) {
        return (int) this.list.stream()
          .filter(event -> event.getLoggerName().contains(loggerName))
          .count();
    }

    public List<ILoggingEvent> search(String string) {
        return this.list.stream()
          .filter(event -> event.toString().contains(string))
          .collect(Collectors.toList());
    }

    public List<ILoggingEvent> search(String string, Level level) {
        return this.list.stream()
          .filter(event -> event.toString().contains(string)
            && event.getLevel().equals(level))
          .collect(Collectors.toList());
    }

    public int getSize() {
        return this.list.size();
    }

    public List<ILoggingEvent> getLoggedEvents() {
        return Collections.unmodifiableList(this.list);
    }
}

The MemoryAppender class handles a List that is automatically populated by the logging system.

It exposes a variety of methods to cover a wide range of test purposes:

  • reset() – clears the list
  • contains(msg, level) – returns true only if the list contains an ILoggingEvent matching the specified content and severity level
  • countEventForLoggers(loggerName) – returns the number of ILoggingEvent generated by the named logger
  • search(msg) – returns a List of ILoggingEvent matching the specific content
  • search(msg, level) – returns a List of ILoggingEvent matching the specified content and severity level
  • getSize() – returns the number of ILoggingEvents
  • getLoggedEvents() – returns an unmodifiable view of the ILoggingEvent elements

3.2. Asserting Log Statements

Next, let’s create a JUnit test for our business worker.

We’ll declare our MemoryAppender as a field and programmatically inject it into the log system. Then, we’ll start the appender.

For our tests, we’ll set the level to DEBUG:

private final MemoryAppender memoryAppender = new MemoryAppender();

@Before
void setup() {
    Logger logger = (Logger) LoggerFactory.getLogger(LOGGER_NAME);
    logger.setLevel(Level.DEBUG);
    logger.addAppender(memoryAppender);

    memoryAppender.setContext((LoggerContext) LoggerFactory.getILoggerFactory());
    memoryAppender.start();
}

Now we can create a simple test where we instantiate our BusinessWorker class and call the generateLogs method. We can then make assertions on the logs that it generates:

BusinessWorker worker = new BusinessWorker();
worker.generateLogs(MSG);
assertThat(memoryAppender.countEventsForLogger(LOGGER_NAME)).isEqualTo(4);
assertThat(memoryAppender.search(MSG, Level.INFO)).hasSize(1);
assertThat(memoryAppender.contains(MSG, Level.TRACE)).isFalse();

This test uses three features of the MemoryAppender:

  • Four logs have been generated — one entry per severity should be present, with the trace level filtered
  • Only one log entry with the content message with the level severity of INFO
  • No log entry is present with content message and severity TRACE

If we plan to use the same instance of this class inside the same test class when generating a lot of logs, the memory usage will creep up. We can invoke the MemoryAppender.clear() method before each test to free memory and avoid OutOfMemoryException.

In this example, we’ve reduced the scope of the retained logs to the LOGGER_NAME package, which we defined as “com.baeldung.junit.log“. We could potentially retain all logs with LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME), but we should avoid this whenever possible as it can consume a lot of memory.

3.3. Handling Multiple Logs with Varying Levels

To validate application logs effectively in scenarios where multiple entries are generated at various levels (INFO, WARN, ERROR, and more), we can use methods such as counting, filtering, and asserting specific content:

BusinessWorker worker = new BusinessWorker();
worker.generateLogs("Transaction started for Order ID: 1001");

assertThat(memoryAppender.countEventsForLogger(LOGGER_NAME)).isEqualTo(4);
assertThat(memoryAppender.search("Transaction started", Level.DEBUG)).hasSize(1);
assertThat(memoryAppender.search("Transaction started", Level.INFO)).hasSize(1);
assertThat(memoryAppender.search("Transaction started", Level.WARN)).hasSize(1);
assertThat(memoryAppender.search("Transaction started", Level.ERROR)).hasSize(1);
assertThat(memoryAppender.search("Transaction started", Level.TRACE)).isEmpty();

In the @Before setup method, the logger’s level is programmatically set to DEBUG. A logger set to DEBUG will log everything from DEBUG to higher levels (INFO, WARN, ERROR), but it will exclude TRACE logs because TRACE has a lower priority than DEBUG.

3.4. Using Pattern Matching for Dynamic Log Content

When log messages contain dynamic data like timestamps, user IDs, or transaction IDs, we can use regular expressions to validate their content. This ensures that the structure and key details of the log messages are correct.

Let’s create a method for pattern-based validation:

boolean containsPattern(Pattern pattern, Level level) {
    return this.list.stream()
      .filter(event -> event.getLevel().equals(level))
      .anyMatch(event -> pattern.matcher(event.getFormattedMessage()).matches());
}

This utility method provides a clean way to check if any log message matches a given pattern at a specific level.

Let’s demonstrate how to validate log messages with dynamic content using regex:

BusinessWorker worker = new BusinessWorker();
worker.generateLogs("Order processed successfully for Order ID: 12345");

Pattern orderPattern = Pattern.compile(".*Order ID: \\d{5}.*");

assertThat(memoryAppender.containsPattern(orderPattern, Level.DEBUG)).isTrue();
// ... assert presence of pattern in INFO, WARN, and ERORR log levels as well 
assertThat(memoryAppender.containsPattern(orderPattern, Level.TRACE)).isFalse();

The regex .*Order ID: \\d{5}.* ensures the message contains an order ID followed by exactly 5 digits. With this, we can ensure the test remains valid even if the dynamic part of the log (order ID) changes.

In cases where multiple patterns are expected, we can create a containPatterns() method for matching multiple patterns:

boolean containsPatterns(List<Pattern> patternList, Level level) {
    return patternList.stream()
      .allMatch(pattern -> containsPattern(pattern, level));
}

Then, we can validate all of them in a single test:

BusinessWorker worker = new BusinessWorker();
worker.generateLogs("User Login: username=user123, timestamp=2024-11-25T10:15:30");

List patterns = List.of(
  Pattern.compile(".*username=user\\w+.*"),
  Pattern.compile(".*timestamp=\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}.*")
);

assertThat(memoryAppender.containsPatterns(patterns, Level.DEBUG)).isTrue();
// ... assert presence of patterns in INFO, WARN, and ERORR log levels as well
assertThat(memoryAppender.containsPatterns(patterns, Level.TRACE)).isFalse();

4. Asserting Logs in Spring Boot With OutputCaptureExtension

For Spring Boot applications, we have a built-in solution that we can use to assert logs in our tests.

Let’s start by extending our test class with the OutputCaptureExtension:

@ExtendWith(OutputCaptureExtension.class)
class BusinessWorkerUnitTest {
}

The OutputCaptureExtension captures the output written to System.out and System.err during the execution of each test method.

We can then inject the captured output into our test method using the CapturedOutput parameter:

@Test
void whenLogsGenerated_thenCapturedOutputContainsLogs(CapturedOutput capturedOutput) {
    String log = "Order processed successfully for Order ID: 12345.";
    BusinessWorker worker = new BusinessWorker();
    worker.generateLogs(log);

    assertThat(capturedOutput.getOut()).contains(log);
    assertThat(capturedOutput.getOut()).containsPattern(".*Order ID: \\d{5}.*");
}

Here, we first generate logs using our familiar generateLogs() method. Then, we assert the presence of our log message in the capturedOutput parameter.

To validate dynamic content in the logs, we use AssertJ’s built-in containsPattern() method.

It’s important to note that we don’t have an option to assert the log level in this approach. One might argue that it’s part of the underlying logging framework and we shouldn’t test it anyway.

The OutputCaptureExtension simplifies log assertions in Spring Boot tests by automatically capturing the output and providing it to our test methods. This eliminates the need for custom appenders or configuring the logging system.

5. Conclusion

In this article, we’ve explored asserting log messages in our JUnit tests, covering two approaches, both in native Java and Spring Boot applications.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)