@Repository
publicclassMyBatisAngelWangimplementsIRepository{
@Autowired
privateGeneralDAO generalDAO;
public<T extendsBase> T get(Class<T> clz,Long id){
HashMap hashMap = generalDAO.getLogically(clz, id);
T ret =this.convert(hashMap, clz);
return ret;
}
}
import org.testng.annotations.Test;
//@DatabaseSetup(value= "/dbunitData/TestAngelEntity.xml")
publicclassMyBatisAngelWangTestextendsAbstractRollbackTest{
@Autowired
privateMyBatisAngelWang myBatisAngelWang;
@Test(enabled =false)
publicvoid testGet(){
}
}
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.TestExecutionListeners;
@ContextConfiguration(locations ={"classpath:spring-datasource-dbunit.xml",
"classpath*:spring-services.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class,
TransactionDbUnitTestExecutionListener.class,TransactionalTestExecutionListener.class})
@Transactional
publicclassAbstractRollbackTestextendsAbstractTestNGSpringContextTests{
}
@ContextConfiguration("/config/Spring-db.xml")
@Transactional
@ActiveProfiles("test")
publicclassMyBatisAngelWangTestextends
AbstractTransactionalTestNGSpringContextTests{}
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
default-autowire="byName">
<description>spring-datasource-configuration</description>
<beanclass="com.angel.context.ApplicationContextAwareHelper"/>
<!-- 定义事务管理器(声明式的事务) -->
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
<beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<propertyname="locations">
<list>
<value>classpath*:props/datasource_dev.properties</value>
</list>
</property>
</bean>
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="${jdbc.driver}"/>
<propertyname="url"
value="${jdbc.dbunit.url}"/>
<propertyname="username"value="${jdbc.user}"/>
<propertyname="password"value="${jdbc.password}"/>
</bean>
<!-- MyBatis 配置 -->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.angel.*.dao"/>
<propertyname="sqlSessionFactoryBeanName"value="xSqlSessionFactory"/>
</bean>
<beanid="xSqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="typeAliasesPackage"value="com.angel.*.entities"/>
<propertyname="configLocation"value="classpath:mybatis/mybatis-config.xml"/>
<propertyname="mapperLocations"value="classpath:/com/angel/dao/*.xml"/>
<propertyname="plugins">
<array>
<!--page interceptor-->
<beanclass="com.angel.orm.db.QueryInterceptor"/>
</array>
</property>
</bean>
<tx:annotation-driventransaction-manager="transactionManager"/>
</beans>
这样大家测试的数据库都是同一个了,也不会有任何的相互影响了。因为事务回滚了,不信的话可以提交一条Insert测试哦,执行完后查看数据库中并没有插入任何数据。然而,在一个单元测试中,先Insert再get是可以取到数据的,神奇吧?!
publicclassApplicationContextAwareHelperimplementsApplicationContextAware{
privatestaticApplicationContext context;
@Override
publicvoid setApplicationContext(ApplicationContext applicationContext){
context = applicationContext;
}
publicstaticApplicationContext getContext(){
return context;
}
}
DruidDataSource dataSource =ApplicationContextAwareHelper.getBean("dataSource_"+ dataSources[i]);
当然,这不属于单元测试的范畴了,有点跑题,但是蛮有用的,在这里记一下。