2010-07-11 29 views
2

我正在使用Hibernate/Spring/Maven/MySQL和JUnit進行單元測試。直到昨天,即使在測試運行完成之後,我的測試數據仍然保留在數據庫中。我在今天配置了這個地獄,所有的數據在每次測試後都會被刪除。很確定,這不是bug,而是一個配置問題。不過,我迷路了。在使用Hibernate/Spring/Maven/MySQL進行測試之後,防止數據被刪除

appContext.xml:

<?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 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd" 
     xmlns:context="http://www.springframework.org/schema/context" 
     xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
     xmlns:tx="http://www.springframework.org/schema/tx" 
     xmlns:util="http://www.springframework.org/schema/util"> 

     <tx:annotation-driven/> 
    <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> 

    <bean class="org.springbyexample.util.log.AnnotationLoggerBeanPostProcessor" /> 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="locations"> 
      <value>classpath:/settings.properties</value> 
     </property> 
    </bean> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${driver}"/> 
     <property name="url" value="${url}"/> 
     <property name="username" value="${user}"/> 
     <property name="password" value="${password}"/> 
    </bean> 

     <bean id="entityManagerFactory" 
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="persistenceUnitName" value="RDBMS"/> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="generateDdl" value="false"/> 
       <property name="showSql" value="true"/> 
       <property name="databasePlatform" value="${databasePlatformDialect}"/> 
       <property name="database"> 
        <util:constant static-field="${databaseVendor}" /> 
       </property> 
      </bean> 
     </property>  
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
    </bean> 

    <context:component-scan base-package="de.test"> 
     <context:exclude-filter type="regex" expression="de\.sandbox\.test\.hibernatedao.*"/> 
    </context:component-scan> 

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> 
    </beans> 

的persistence.xml:

<persistence xmlns="http://java.sun.com/xml/ns/persistence" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
      version="1.0"> 

    <persistence-unit name="RDBMS" transaction-type="RESOURCE_LOCAL"> 
     <exclude-unlisted-classes>true</exclude-unlisted-classes> 
    </persistence-unit> 
</persistence> 

感謝您的建議。

編輯---- 所要求的測試用例:

package de.test.base; 

import org.junit.runner.RunWith; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
import org.springframework.transaction.annotation.Transactional; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration("/appContextMain.xml") 
@Transactional 
public abstract class SpringTestCase { 
} 

兒童:

package de.test.dao; 

import de.test.base.SpringTestCase; 
import de.test.businessobjects.BodSt; 
import de.test.businessobjects.Trainee; 
import junit.framework.Assert; 
import org.junit.Before; 
import org.junit.Test; 
import org.slf4j.Logger; 
import org.springbyexample.util.log.AutowiredLogger; 
import org.springframework.beans.factory.annotation.Autowired; 

import java.util.Collection; 
import java.util.List; 

public class BodStDAOTest extends SpringTestCase { 

    @AutowiredLogger 
    final Logger logger = null; 

    @Autowired 
    private IBodStDAO bodStDAO; 

    @Autowired 
    private ITraineeDAO traineeDAO; 

    Trainee trainee = new Trainee(); 

    @Before 
    public void onSetUpInTransaction() throws Exception { 

     this.trainee.setName("Name"); 
     this.trainee.setSurname("Surname"); 
     this.trainee = this.traineeDAO.save(this.trainee); 
    } 

    @Test 
    public void testSingleObjectSave() throws Exception { 

     Collection before = (List) this.bodStDAO.getAll(); 

     BodSt bodSt = new BodSt(); 
     bodSt.setWeight((float) 2.2); 
     bodSt.setHeight(new Float(0.0)); 
     bodSt.setTrainee(trainee); 
     bodSt = this.bodStDAO.save(bodSt); 

     Collection after = (List) this.bodStDAO.getAll(); 

     this.logger.info("BodSt size before: " + before.size() + " and after: " + after.size()); 
     Assert.assertEquals(before.size() + 1, after.size()); 
    } 
} 

回答

2

在創建測試數據的測試用例上使用@Rollback(value = false)註釋可確保數據不會被刪除。

+0

謝謝,這樣做! – 2010-07-16 09:34:56

+0

如果您還有多個transactionManagers,則可以使用public @interface TransactionConfiguration。 – Koitoer 2014-01-28 07:20:18

0

你能來運行測試的一個交易內,rollbacking呢?

+0

err ...對不起,我仍然是一個初學者......聽起來合乎邏輯,但是,我怎麼能確定這一點? – 2010-07-11 23:09:57

+0

您是否將您的測試標記爲@Transactional?如果他們被標記爲@Transactional,那麼測試會在數據執行後將數據回滾。 – 2010-07-12 00:21:41

+0

@erlord:本提供了一個答案。也許你可以給這個問題添加一個有代表性的測試用例。 – 2010-07-12 01:25:18

相關問題