I have webapp using integrated spring and mybatis . @Transaction works fine in spring mvc , but not in my unit test .
code is worth than thousand words ,
application-test.xml
<context:annotation-config/>
<context:component-scan base-package="com.isildur"/>
<!-- middleware datasource -->
<bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/istaritest"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--<property name="configLocation" value="SqlMapConfig.xml" />-->
<!--<property name="typeAliasesPackage" value="com.isildur.security.entity"/>-->
<property name="mapperLocations" value="classpath:/mapper/**/*.xml"/>
</bean>
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory" />
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.isildur"/>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource"/>
</bean>
unit test
@ContextConfiguration(locations = {"classpath:application-test.xml"})
public class CustomerRepoTest extends AbstractTestNGSpringContextTests {
@Autowired
CustomerRepo customerRepoImpl;
@Transactional(rollbackFor = Exception.class)
public void testCreateCustomer() throws Exception {
Customer cust = new Customer();
cust.setAccountCode("2");
cust.setMobileNumber("3");
cust.setEmailAddress("ad@gmail.com");
customerRepoImpl.save(cust);
Customer cust2 = new Customer();
cust2.setAccountCode("5");
cust2.setMobileNumber("3");
cust2.setEmailAddress("3@gmal.com");
customerRepoImpl.save(cust2);
throw new Exception("canceled");
}
}
output in console
JDBC Connection [com.mysql.jdbc.JDBC4Connection@44536de4] will not be managed by Spring
it should be canceled because an exception . but it still insert into db .
Did i miss the configuration ?
any ideas are welcome .
thanks .
Aucun commentaire:
Enregistrer un commentaire