Search code examples
javaspring-bootlogginglog4j2spring-boot-test

Extend Log4j2 Tests


I have a wrapper for log4j2 Logger class - e.g.

import org.apache.logging.log4j.core.Logger;
       
public class MyLogger extends Logger {
    protected MyLogger(LoggerContext context, String name, MessageFactory messageFactory) {
        super(context, name, messageFactory);
    }

    ....
    
}

I am trying to extend the LoggerTest class to verify MyLogger functionality (so I can run the contained test and add new ones). I've tried something like the following:

@SpringBootTest
@LoggerContextSource(
    value = "log4j-test2.xml",
    reconfigure = ReconfigurationPolicy.AFTER_EACH
)
public class MyLoggerTest extends LoggerTest {
    public MyLoggerTest(LoggerContext context, ListAppender app, ListAppender host, ListAppender noThrown) {
        super(context, app, host, noThrown);
    }
}

org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.apache.logging.log4j.test.appender.ListAppender arg1] in constructor....

I tried creating a local implementation of @LoggerContextSource and the property resolver/extensions classes it references (@Extensions({@ExtendWith({LoggerContextResolver.class}), @ExtendWith({ConfigurationResolver.class}), @ExtendWith({AppenderResolver.class})})) and referencing those instead e.g

@MyLoggerContextSource(
    value = "log4j-test2.xml",
    reconfigure = ReconfigurationPolicy.AFTER_EACH
)
...
@Extensions({
   @ExtendWith({MyLoggerContextResolver.class}), 
   @ExtendWith({MyConfigurationResolver.class}), 
   @ExtendWith({MyAppenderResolver.class})
})
public @interface MyLoggerContextSource {
...
}

but get Duplicate ParameterResolver... error for LoggerContextResolver and MyLoggerContextResolver

Is there anyway I can extend the log4j2 tests?


Solution

  • Thanks to @rgoers, turns out I was missing the @Named annotation on the parameters.

    So my MyLoggerTest works with the following:

    @SpringBootTest
    @LoggerContextSource(
        value = "log4j-test2.xml",
        reconfigure = ReconfigurationPolicy.AFTER_EACH
    )
    public class MyLoggerTest extends LoggerTest {
        public MyLoggerTest(final LoggerContext context, @Named("List") final ListAppender app, @Named("HostTest") final ListAppender host, @Named("NoThrowable") final ListAppender noThrown) {
            super(context, app, host, noThrown);
        }
    }