Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate SLF4J Binding or Config For Unit Test?

Tags:

java

maven

slf4j

I'd like to be able to switch the bindings for SLF4J for unit testing over to the simple version, but it seems there is no easy way to config this. Basically, my built project logs to a file and I'd like it to be console instead when unit testing.

I've used the antrun maven plugin before to do something like this with persistence.xml files, but that solution seems a bit heavy handed.

Does anyone out there have a solution to using alternate configs or bindings in unit tests?

TIA

like image 603
javamonkey79 Avatar asked May 05 '11 19:05

javamonkey79


1 Answers

A better approach is to facilitate your logging framework. Logback library for instance first searches for logback-test.xml and if it is not available, it looks for logback.xml. If you place logback-test.xml in /src/test/resources, it will be picked up for unit tests. In this file you configure console logging instead of file.

If you are still using Log4J, simply place log4j.xml in /src/test/resources - this folder is available on classpath before /src/main/resources, so Log4J will use it instead of ordinary /src/main/resources version, while still loading the latter version for final builds (/src/test/resources isn't even available then).

like image 192
Tomasz Nurkiewicz Avatar answered Nov 11 '22 10:11

Tomasz Nurkiewicz