求破:spring+ibatis的junit测试报找不到配置文件

leijian_sctel 2013-08-19 10:16:35
报错信息:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlMapClient' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is java.io.FileNotFoundException: class path resource [WEB-INF/sql-map-config.xml] cannot be opened because it does not exist

实际上在“WEB-INF/sql-map-config.xml”目录下是有此文件的,用tomcat都能debug通过,用junit测试就不行

applicationContext.xml的程序段
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="configLocation">
<value>/WEB-INF/sql-map-config.xml</value>
</property>
</bean>

<bean id="userdao" class="com.dao.UserDaoImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient"/>
</property>
</bean>

<bean id="userservice" class="com.service.UserServiceImpl">
<property name="userdao">
<ref bean="userdao" />
</property>
</bean>

junit程序代码
package cn.com.sctel.junit;

import static org.junit.Assert.fail;

import java.util.Date;

import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;
import org.springframework.test.context.transaction.TransactionConfiguration;

import com.domain.User;
import com.service.UserService;

/** 设置要加载的配置文件 */
@ContextConfiguration(locations = { "classpath:applicationContext.xml" })
/* * 设置是否回滚数据 */
@TransactionConfiguration(defaultRollback = false)
public class UserTest extends AbstractTransactionalJUnit4SpringContextTests {
private UserService userservice;
private User user;
/** 设置自动注入的属性 */
@Autowired
// private IUserService userService;
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
@Rollback(false)
public void testDelete() throws Exception {
user.setId(1);
user.setPassword("222");
user.setUsername("leijian");
boolean b = userservice.isDelete(user);
if (b) {
System.out.println("success");
} else {
System.out.println("false");
}
}
}


...全文
227 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
leox_2010 2014-01-17
  • 打赏
  • 举报
回复
一楼正解,你试一下
ImproveMe 2014-01-16
  • 打赏
  • 举报
回复
很简单,把sql-map-config.xml放到根目录下,junit的操作是针对src目录,所以web-Info下是不认的,放在src下,配置文件路径改一下就行了。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧