当前位置:编程学习 > JAVA >>

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

报错信息:
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");
}
}
}


补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,