For a Mule 4 EE app, I am writing integration tests using JUnit4 and Rest Assured. In a test I am trying to verify that certain messages are logged by the app. How can I do that?
I wrote a customer appender and attached it to the root logger, but it is not getting any messages. Update - that's not working because the Mule runtime is running in a separate process and the test is not able to tap into the runtime's logger.
static class TestAppender extends AppenderSkeleton {
public List<org.apache.log4j.spi.LoggingEvent> events =
new ArrayList<org.apache.log4j.spi.LoggingEvent>();
public void close() {}
public void clearLog() {
events.clear();
}
public boolean requiresLayout() {
return false;
}
@Override
protected void append(LoggingEvent event) {
events.add(event);
}
}
and the test
// is not working
@Test
public void test_log_msg() throws Exception {
TestAppender testAppender = new TestAppender();
Logger.getRootLogger().addAppender(testAppender);
// perform some action that causes logging
assertEquals(testAppender.events.size(), 1);
}
Any help will be greatly appreciated.
Specifically, I am trying to verify that caching is working for my API. I was going to do that by verifying the messages that I am logging in the app when the caching happens. If there is a more elegant approach to doing it, please let me know.
I don't think this is a good test design. Logging is not meant for unit testing checks. The test will be easy to break. Also technically the implementation in Mule 4 as you mentioned is not easy or possible to capture in that way. Add to it that currently Mule 4 only supports MUnit, not JUnit.
I would recommend to go back a step and think what your test should be really validating, which is the caching and not the logging. Try to implement it using MUnit if possible.