我为我的测试定义了以下内容:
@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{
...
}
在web.xml中,我使用相同的配置和其他配置,例如配置过滤器和监听器。如何在进行测试时启用这些功能?
如果Spring使用相同的 web.xml 会很棒,但我想这可能不是一个选择。
我在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>
有没有办法从contextConfig.xml(bean定义)配置日志记录?
答案 0 :(得分:1)
http://codebyexample.info/2012/03/31/how-log4j-and-junit-became-friends/
可能会有所帮助,尤其是作者所做的部分:
Logger.getRootLogger().addAppender(someAppender)
您应该能够访问Logger.getRootLogger()并在那里进行配置。 如果要使用属性文件,可以执行以下操作:
PropertyConfigurator.configure("/config/log4j.properties");
How to configure log4j.properties for SpringJUnit4ClassRunner?
或手动配置它:
Logger rootLogger = Logger.getRootLogger();
rootLogger.setLevel(Level.INFO);
rootLogger.addAppender(new ConsoleAppender(
new PatternLayout("%-6r [%p] %c - %m%n")));
这些配置应该在所有课程运行之前完成一次,但是如果你在@Before方法中进行这些配置,我认为它们不会造成太大的伤害。