在测试类中,需要导入applicationContext.xml,才能把数据插入到数据库测试表中,
测试时,出现如下错误:
log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext).
log4j:WARN Please initialize the log4j system properly.
Junit Failure Trace
org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [WebRoot/WEB-INF/applicationContext.xml]; nested exception is java.io.FileNotFoundException: class path resource [WebRoot/WEB-INF/applicationContext.xml] cannot be opened because it does not exist
Caused by: java.io.FileNotFoundException: class path resource [WebRoot/WEB-INF/applicationContext.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:135)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:307)
........
解决办法:
(1)applicationContext.xml一般放在WebRoot/WEB-INF/下,使用FileSystemXmlApplicationContext能正确获取到applicationContext.xml
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
ApplicationContext ctx = new FileSystemXmlApplicationContext(
"WebRoot/WEB-INF/applicationContext.xml");
clothingdao = (ClothingDao )ctx.getBean("clothingdao");
}
(2)applicationContext.xml一般放在WebRoot/WEB-INF/下,直接使用ClassPathXmlApplicationContext时还会报错,
只需要将applicationContext.xml直接放入src文件目录下即可正确获取applicationContext.xml
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"WebRoot/WEB-INF/applicationContext.xml");
clothingdao = (ClothingDao )ctx.getBean("clothingdao");