Ask Your Question

Revision history [back]

In order to perform a JUnit assertion on a logger message, you can use the following steps:

  1. Create a new Logger object.
  2. Use the Logger object to print a message.
  3. Create an instance of the JUnit Assertion class.
  4. Retrieve the logger messages using the Logger object.
  5. Use the Assertion class to compare the logger message with the expected value.

Here is an example code snippet:

import org.junit.Assert;
import org.junit.Test;
import java.util.logging.Logger;

public class LoggerTest {

  @Test
  public void testLogger() {
    Logger logger = Logger.getLogger(LoggerTest.class.getName());
    logger.info("Hello World!");
    String actualMessage = logger.getMemoryHandler().getPushedRecords().get(0).getMessage().toString();
    String expectedMessage = "Hello World!";
    Assert.assertEquals(expectedMessage, actualMessage);
  }
}

In this example, we create a new Logger object, print a message, retrieve the logger message, and use the Assertion class to compare the message with the expected value. If the actual message matches with the expected message, the assertion will pass.