1 | initial version |
In order to perform a JUnit assertion on a logger message, you can use the following steps:
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.