开发者

Prevent data from being deleted after test run with Hibernate / Spring / Maven / MySQL

I am using Hibernate / Spring / Maven / MySQL and unit tests with JUnit. Up to yesterday, my testdata persisted in the database even after the test run was completed. I configured the hell out of this day and all of the sudden all data are being deleted after every test run. Quite sure, this is no bug, but a config issue. Nevertheless, I am lost.

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>

Thanks for suggestions.

EDIT ---- As demanded, the testcase:

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 {
}

Child:

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.uti开发者_开发知识库l.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());
    }
}


Using @Rollback(value = false) annotation on your test case that creates the test data makes sure that the data doesnt get deleted.


Could you be running your tests inside a transaction and rollbacking it?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜