spring中JUnit4测试中,AbstractTransactionalJUnit4SpringContextTests报错关于javax.sql.DataSource

来源:互联网 发布:mac os u盘启动盘 编辑:程序博客网 时间:2024/06/03 18:37

今天使用AbstractTransactionalJUnit4SpringContextTests,进行spring集成的hibernat测试

package bijian.model.dao.hibernateImpl;import java.util.List;import javax.annotation.Resource;import org.hibernate.SessionFactory;import org.junit.Assert;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.annotation.Rollback;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import org.springframework.test.context.transaction.TransactionConfiguration;import bijian.model.bean.User;import bijian.model.dao.IUserDao;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"classpath:spring.xml"})@TransactionConfiguration(transactionManager="transactionManager",defaultRollback=true)public class UserDaoImplTests extends AbstractTransactionalJUnit4SpringContextTests{@Resource(name="userDao",type=UserDaoImpl.class)    private IUserDao userDao;@Rollback(false)@Testpublic void testInsert(){//T entityUser user=new User();user.setUsername("jazywoo");user.setNickname("wujianzhi");user.setPassword("123456");userDao.insert(user);System.out.println(user.getUserID());Assert.assertNotNull("userID not null", user.getUserID());}    }@Testpublic void testGetByUsername(){//String usernameUser user=new User();user.setUsername("jazywoo");user.setNickname("wujianzhi");user.setPassword("123456");userDao.insert(user);String username="jazywoo";User user2=(User) userDao.get(username);System.out.println(user.getUserID());Assert.assertNotNull(user2);}    }

于是报错是

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests.setDataSource(javax.sql.DataSource); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:601)at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)... 26 moreCaused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:949)at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:730)at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:558)... 28 more
原因是找不到配合文件中的dataSource,由于集成的hibernate,使用的是hibernate.hbm.xml的配置信息,没有配置dataSource;

解决方法有两种,

一种是去掉AbstractTransactionalJUnit4SpringContextTests,不继承,可以直接测试,但是使用@Rollback(false)的时候,事件还是会回滚。

一种是是在appplicationContext.xml中配置上dateSource数据源,这时候使用@Rollback(false)的时候,事件才不会回滚,会写入数据库。

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">         <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="jdbc:mysql://localhost/db_sentence" />        <property name="username" value="root" />        <property name="password" value="admin" />             </bean>     <!-- 定义sessionFactory -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">       <property name="configLocations">  <value>classpath:hibernate.cfg.xml</value>   </property>    </bean>        <!-- 定义事务管理器:此处的含义就是下面的事务管理器管理由sessionFactory创建的session -->    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory" ref="sessionFactory" />    </bean>        <!-- HibernateTemplate类是Spring提供给我们进行Hibernate持久层操作的类,它对增删查改方法进行了封装 -->    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">            <property name="sessionFactory" ref="sessionFactory"/>        </bean>             <bean id="userDao" class="bijian.model.dao.hibernateImpl.UserDaoImpl">            <property name="hibernateTemplate" ref="hibernateTemplate"/>       </bean>       <bean id="userRelatedObjectDao" class="bijian.model.dao.hibernateImpl.UserRelatedObjectDaoImpl">            <property name="hibernateTemplate" ref="hibernateTemplate"/>       </bean>       </beans>



















原创粉丝点击