0

I have the following defined for my test:

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration("/App/webapp")
@ContextConfiguration({
        "classpath:/config/AConfig.xml",
        "classpath:/config/BConfig.xml",
        "classpath:/config/CConfig.xml",
        "classpath:/config/DConfig.xml"
})
public class MyTests{
    ...
} 

In web.xml I use the same configuration plus additional configuration, for instance to configure filters and listeners. How can I enable these when doing tests?

It would be great if Spring just used the same web.xml but I guess that might not be an option.

I have this defined in web.xml:

<context-param>
    <param-name>log4jConfigLocation</param-name>
    <param-value>classpath:/config/log4j.properties</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

Is there a way to configure logging from a a contextConfig.xml ( bean definition ) instead?

mjs
  • 21,431
  • 31
  • 118
  • 200

1 Answers1

1

http://codebyexample.info/2012/03/31/how-log4j-and-junit-became-friends/

that may be helpful, in particular the part where the author does:

Logger.getRootLogger().addAppender(someAppender)

You should be able to access Logger.getRootLogger() and configure it there. If you want to use the properties file, you can do:

PropertyConfigurator.configure("/config/log4j.properties");

How to configure log4j.properties for SpringJUnit4ClassRunner?

or manually configure it like:

Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.INFO);
rootLogger.addAppender(new ConsoleAppender(
       new PatternLayout("%-6r [%p] %c - %m%n")));

These configurations should be done once before all classes are run, though I don't think they harm too much if you do them in a @Before method.

Community
  • 1
  • 1
Joeblade
  • 1,735
  • 14
  • 22
  • I opted for the PropertyConfigurator, however, it requires a full absolute path. – mjs Oct 26 '14 at 09:26
  • 1
    You might be able to use the syntax classpath:/a/b/c – Joeblade Oct 26 '14 at 13:13
  • If you're using maven, you might need to make sure the file is in the test resources folder (or src resources folder). have a look at: http://forum.spring.io/forum/spring-projects/container/26798-classpath-context-xml-with-file-system-properties (which refers to: docs.spring.io/spring-framework/docs/2.5.6/api/org/springframework/core/io/ResourceLoader.html and http://stackoverflow.com/questions/9092713/using-classpath-in-spring – Joeblade Oct 26 '14 at 19:27