每次运行单元测试时都在擦除我的 H2 数据库是啥?

Posted

技术标签:

【中文标题】每次运行单元测试时都在擦除我的 H2 数据库是啥?【英文标题】:What is wiping my H2 database every time I run a unit test?每次运行单元测试时都在擦除我的 H2 数据库是什么? 【发布时间】:2013-04-08 02:05:01 【问题描述】:

我有一个 Spring+Hibernate+H2 项目,是根据我在 Internet 上找到的示例制作的。它工作得很好,除了每次我运行单元测试时,数据库都会被擦除。我不确定是什么原因造成的。测试通过了,但是我在测试之前放入数据库的任何东西在测试运行后都会被清除。

任何想法都会有所帮助!谢谢!

这是我的基础设施.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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc     
    http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
                    http://www.springframework.org/schema/beans 

    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="generateDdl" value="true" />
            <property name="database" value="H2" />
        </bean>
    </property>
    <property name="persistenceUnitName" value="booksrus" />

</bean>

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

<bean id="dataSource" 
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="org.h2.Driver"/>
  <property name="url" value="jdbc:h2:tcp://localhost:9092/mydb"/>
  <property name="username" value=""/>
  <property name="password" value=""/>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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_2_0.xsd">
    <persistence-unit name="booksrus">      
        <properties>
            <property name="hibernate.hbm2ddl.auto" value="create" /> 
        </properties>
    </persistence-unit>
</persistence>

Junit 测试

package bookstore;

import static org.junit.Assert.*;

import java.util.List;

import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;

import stemen.entity.User;
import stemen.repository.UserRepository;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:simple-repository-context.xml")
@Transactional
public class TestUserRepository 
    private final Logger LOGGER = Logger.getLogger(TestUserRepository.class);

    @Autowired
    UserRepository repository;
    private User tom;
    private User patrick;
    private User john;

    @Before
    public void setUp() 
        tom = new User("123 California", "Apt 143", "LA", "Tom@gmail.com", "Tom", "Hanks", "Itsasecret", "CA","54221");
        patrick = new User("847 Mapple Dr.", "", "Washington", "Patrick@gmail.com", "Patrick", "Steward", "moneyMonkey", "MD","64541");
        john = new User("8484 Bristol", "", "Columbus", "john@gmail.com", "John", "Roberts", "pass", "OH","57963");
        repository.save(tom);
        repository.save(patrick);
        repository.save(john);
        assertThat(repository.count(), equalTo(3L));
    



    /**
     * Tests inserting a user and asserts it can be loaded again.
     */
    @Test
    public void testThatTomCanBeInserted() 
        User retrievedUser = repository.save(tom);
        assertThat(retrievedUser, equalTo(tom));
        assertEquals(tom, repository.findOne(retrievedUser.getId()));
    

    @Test
    public void testThatJohnCanBeFoundByEmailAndPassword()
        User retreivedUser = repository.findUserByEmailIgnoreCaseAndPassword(john.getEmail(), john.getPassword());
        assertThat(retreivedUser, equalTo(john));
    


【问题讨论】:

【参考方案1】:

这是属性名="hibernate.hbm2ddl.auto" value="create" /> 每次都会删除 n 重新创建模式。 将其更改为更新,因此如果不存在,它只会第一次创建它。

查看此链接了解更多说明: Link

【讨论】:

我按照您的建议将其更改为更新,并且它停止清除数据库。但是,我坚持的实体在测试后并没有最终出现在数据库中。我采用相同的代码并在一个单独的类中使用 main 方法运行它,并且实体出现了。我猜@RunWith(SpringJUnit4ClassRunner.class) 强制对数据库的更改在测试结束时回滚。感谢您的帮助!

以上是关于每次运行单元测试时都在擦除我的 H2 数据库是啥?的主要内容,如果未能解决你的问题,请参考以下文章

Cordova 服务命令只是重置应用程序擦除我的所有更改

为啥 Python .readlines() 方法似乎在擦除文件?

使用 JPA 和 JUnit 测试时如何一致地擦除内存数据库中的 H2 [重复]

对于向量中的指针,我们应该在擦除之前还是之后删除?

Spring Boot with H2 每次 web 应用启动时都会运行 data.sql,这正常吗?

nor flash之擦除和写入