Junit4对SSH Dao层进行单元测试,报错:WARN No appenders could be found for logger (org.springframework.context.su

在进行SSH Dao层的Junit4单元测试时,遇到log4j无法找到appenders的警告以及BeanDefinitionStoreException,原因是无法加载WebRoot/WEB-INF/applicationContext.xml。解决方法包括使用FileSystemXmlApplicationContext从指定路径加载,或者将applicationContext.xml移动到src目录下通过ClassPathXmlApplicationContext加载。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在测试类中,需要导入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)

........


 

解决办法:

  

1applicationContext.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");  

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值