spring下Junit_jdbc回滚demo

配置测试类
  添加如下内容在class前,用于配置applicationContext.xml文件的位置。
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")

***********************
Junit4 不回滚
2012-02-13
 @Rollback
表明被注解方法的事务在完成后是否需要被回滚。 如果true,事务将被回滚,否则事务将被提交。使用@Rollback接口来在类级别覆写配置的默认回滚标志。
@Rollback(false)
public void testProcessWithoutRollback() {
// …
}
@NotTransactional
出现该注解表明测试方法必须不在事务中执行。
@NotTransactional
public void testProcessWithoutTransaction() {
// …
}

*************************************************************
2008-11-24 
在一般的java 项目中,我们对于数据库的操作需要写大量的junit代码,这些测试代码都包含事物。
    在以下情况下:使用插入了一条数据到test database ,每跑一次junit test 都会向test database 里面插入相同的数据。这明显不可取,如果自己写rollback,代码量很大。
那有没有方法实现junit test的回滚,可以使用spring自带的事物管理来解决。
以下为步骤:
1:配置DataSourceTransactionManager
 <!-- config transaction -->
     <bean id="txManager"
         class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
         <property name="dataSource" ref="dataSource" />
     </bean>

dataSource为数据库配置:(这里使用了变量配置数据库的用户名和密码)
 <bean id="dataSource"
         class="org.apache.commons.dbcp.BasicDataSource"
         destroy-method="close">
         <property name="url" value="${jdbc.url}" />
         <property name="username" value="${jdbc.username}" />
         <property name="password" value="${jdbc.password}" />
         <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="testOnBorrow" value="${jdbc.testOnBorrow}" />         
 </bean>

解析来读取配置文件的配置(注意value里面的路径可以使相对和绝对路径)
  <!--  Reading configuration file -->
     <bean id="propertyConfigurer"
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="locations">
             <list>
                 <value>classpath*:resources/jdbc.properties</value>
             </list>
         </property>
     </bean>
jdbc.properties 的配置如下:
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
 jdbc.url=jdbc:oracle:thin:@0.0.0.0:1521:orcl
 jdbc.username=userName
 jdbc.password=password
 jdbc.testOnBorrow=true

以上是spring 数据源的配置。
2:junit class 写法 TestClass.java
/**固定写法 */ 
@RunWith(SpringJUnit4ClassRunner.class)
 /**txManage是配置的事物管理Object  defaultRollback = true 任何情况都回滚*/
@TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
 /**读取配置文件到运行环境。注意:file的路径 */
@ContextConfiguration(locations = { "file:web/WEB-INF/applicationContext*.xml" })
 @Transactional
 public abstract class TestPersonService {
     /**声明环境里面的bean */            
    @Autowired
    private PersonService personService; 

    @Test
     public void testGetPerson(){
         int userId = 1;
        Person person= personService.getPerson(userId );
         assertNotNull(person);
         assertEquals(userId,person.getUserId());
    }  

}
使用了设置断点的junit写法。下面为非回滚junit class类写法。
private PersonService personService= (PersonService ) OfferMeAppContext.getContext().getBean(
                "personService");

     @Before
     public void setUp() {
         ApplicationContext context = new FileSystemXmlApplicationContext(./web/WEB-INF/applicationContext*.xml)  ;
        this.personService= (PersonService ) context .getContext().getBean(
                "personService");
         dataSource = (DataSource) context .getContext().getBean(
                 "dataSource");
         this.jdbcTemplate = new JdbcTemplate(dataSource);
     }


     
     //@Test
    @Test
    public void testGetPerson(){
        int userId = 1;
        Person person= personService.getPerson(userId );
        assertNotNull(person);
   }
注意:
1:关于配置文件的路径一定要正确
2:spring 配置的object 名字和java类里面写的该类对象的属性名一定要一致
3:spring 的ApplicationContext实现类必须使用ClassPathXmlApplicationContext而不能使用ClassPathResource
ApplicationContext f = new ClassPathXmlApplicationContext("applicationContext.xml");

posted @ 2013-12-01 17:32  linux,dev  阅读(472)  评论(0编辑  收藏  举报