junit 对多数据源写测试

来源:互联网 发布:c语言中&是什么意思 编辑:程序博客网 时间:2024/06/05 21:54

junit 写单元测试的时候,因为项目本身是多数据源的所以在写测试的时候一直提示

No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 3: xx-dataSource xx-dataSource xx-dataSource

spring 在注入的时候无非确认采用哪个数据源

需要指定使用数据源

@Override
   @Resource(name = "xx-dataSource")
   public void setDataSource(DataSource dataSource) {
       // TODO Auto-generated method stub
       super.setDataSource(dataSource);
   }



昨天又进行了实验debug跟踪,发现进行数据源的指定后并没有将本类中所有操作固定为该数据源,对mapper调用仍然使用的是该mapper所属数据源。

换句话说


部分配置文件

<!-- mapperScannerConfigurer -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.acedata.carcoo.mapper.beforeLoan"></property>
<property name="sqlSessionFactoryBeanName" value="beforeLoan-sqlSessionFactory" />
</bean>


<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.acedata.carcoo.mapper.afterLoan"></property>
<property name="sqlSessionFactoryBeanName" value="afterLoan-sqlSessionFactory" />
</bean>

com.acedata.carcoo.mapper.beforeLoan 下的usermapper使用数据源为beforeLoan

com.acedata.carcoo.mapper.afterLoan 下的loanmapper使用数据源为afterLoan

即使在 @Resource(name = "beforeLoan-dataSource")


在测试期间有个问题一直困扰好长时间花费了一天多才找到问题。

在aop在进行增强处理的时候设置的是service实现类但是在junit测试的时候注入的是mapper所以其实spring的事务是没有生效的。走入了一个误区,一直在找的是多数据源进行测试的时候为什么事务没有生效。所以耽误了很长时间。

另外需要注意的一点是多数据源在配置多个数据源时,设置多个事务,同时也需要设置多个增强处理Advice




0 0
原创粉丝点击