小编典典

防止在使用Hibernate / Spring / Maven / MySQL运行测试后删除数据

hibernate

我正在使用Hibernate / Spring / Maven /
MySQL和JUnit进行单元测试。直到昨天,即使测试运行完成,我的测试数据仍保留在数据库中。我从今天开始进行了配置,每次测试运行后突然删除了所有数据。可以肯定的是,这不是错误,而是配置问题。然而,我迷路了。

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());
    }
}

阅读 349

收藏
2020-06-20

共1个答案

小编典典

在用于创建测试数据的测试用例上使用@Rollback(value = false)批注可确保不会删除该数据。

2020-06-20