How to do a JUnit assert on a message in a logger
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
When developing Java applications, testing plays a crucial role in ensuring the code runs as expected. Among various testing frameworks for Java, JUnit is one of the most popular for unit testing. Coupled with assertions for verifying expected results, JUnit helps maintain code reliability. However, testing whether certain expected logs are generated by an application can be equally important, especially when verifying error handling and internal state changes which are reflected only through logging.
To assert log messages in a JUnit test, you can't use JUnit's standard assertions directly because logger messages are typically written to external systems, like console or file systems, and not returned by methods as typical return values. To enable assertions on messages logged during tests, you need to intercept and record these messages first. We can achieve this by using a custom log appender or by utilizing existing libraries which can mock the logging behavior.
Using a Custom Log Appender
A common approach to verify log output is to create a custom log appender. For this example, we are considering Log4j2, a popular logging framework. You can extend the AppenderSkeleton to create an appender that stores log messages in a way that they can be inspected later in the test.
- Create a Custom Appender. Define a class that extends
AppenderSkeleton. In the append method, store the logging events or messages.
- Set Up and Use in JUnit Test. Before your test runs, add this appender to the logger and remove it afterward.
Utilizing Mocking Frameworks
Libraries like Mockito can also mock logger behavior, simplifying the testing of log outputs without modifying the actual logging configuration.
- Mock Logger. Use Mockito to mock a logger and then verify after the method call that the logger was called with the expected level and message.
Summary Table
| Approach | Pros | Cons |
| Custom Log Appender | Full control over log capture; doesn't need third party libraries except logging framework | Additional class code; tightly coupled with specific logging framework |
| Mocking Frameworks | Simpler, less code; better for unit tests focused on behavior | Less suitable if exact formatting or multiple log entries need to be verified |
Each approach has its advantages depending on the test scenario and requirements. For simpler use cases where only the occurrence and level of log messages matter, using Mockito or another mocking library might be preferred. For scenarios where exact log content, order, or format matter, a custom appender provides finer control.
Related reading
- How to do bulk multi row inserts with JpaRepository?
- How to do Multiple URL Mapping aliases in Spring Boot
- How to do PATCH properly in strongly typed languages based on Spring - example
- How to do ToString for a possibly null object?
- How to exclude AutoConfiguration classes in Spring Boot JUnit tests?
- How to extract value from JSON response when using Spring MockMVC
- How to do URL decoding in Java?
- How to dockerize a Maven project? How many ways to accomplish it?

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.