MAREK
MAREK

Reputation: 183

Configure unit tests so that log4j configuration is not required

I want to build spring-boot application with external log4j2 configuration. I created project where log4j config is bundled with spring-boot application jar file, but it is stored outside jar file. Application loads configuration file that is defined in application.properties eg. logging.config=file:config/log4j2.xml. The app structure looks like this:

app.jar 
config:
    ->application.properties
    ->log4j2.xml

App starts correctly and sees both configuration files. However tests can't locate log4j2.xml that is defined in application.properties.

I've created test.properties file as follows:

logging.level.com.mypackage=TRACE

# This sets the global logging level and specifies the appenders
log4j.rootLogger=TRACE, theConsoleAppender

# settings for the console appender
log4j.appender.theConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.theConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.theConsoleAppender.layout.ConversionPattern=%d %p %c{10} [%t] %m%n

and annotated my test class with:

@TestPropertySource(locations="classpath:test.properties")

But test runner still looks for file defined in logging.config.

What properties should I override in application.properties file so that log4j2.xml is not required during unit testing?

Thanks for any help.

Upvotes: 0

Views: 1570

Answers (1)

MAREK
MAREK

Reputation: 183

Best think, I could came up with, was overriding property logging.config to point to log4j configuration contained on classpath and added log4j configuration as test resource.

In my test.properties

logging.config=classpath:log4j2.xml

Upvotes: 1

Related Questions