How to enable log4j when doing spring-based integration test?

Just put a log4j.xml on test’s class path and log4j will be activated.

However, you may hate to have 2 "log4j.xml" in your eclipse. You want the log4j config for testing to be called log4j-test.xml. In that case, do the following:

//create your own runner, withen which load log4j
public class MySpringJunit4ClassRunner extends SpringJUnit4ClassRunner {
	static {
		try {
			Log4jConfigurer.initLogging("classpath:log4j-test.xml");
		} catch (FileNotFoundException ex) {
			System.err.println("Cannot Initialize log4j");
			ex.printStackTrace();
		}
	}

	public PalaSpringJunit4ClassRunner(Class clazz)
			throws InitializationError {
		super(clazz);
	}
}
//And run with your own runner
@RunWith(MySpringJunit4ClassRunner.class)
public class SomeITCase {
...

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.