程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了每次运行单元测试时,要清除H2数据库是什么?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决每次运行单元测试时,要清除H2数据库是什么??

开发过程中遇到每次运行单元测试时,要清除H2数据库是什么?的问题如何解决?下面主要结合日常开发的经验,给出你关于每次运行单元测试时,要清除H2数据库是什么?的解决方法建议,希望对你解决每次运行单元测试时,要清除H2数据库是什么?有所启发或帮助;

这是 ,它每次都会删除n个重新创建的架构。更改它以进行更新,因此它将仅在不存在时才第一次创建。

解决方法

我有一个Spring + Hibernate +
H2项目,它是根据我在Internet上找到的一个示例制作的。除了每次我运行单元测试时,数据库都会被清除外,它的工作效果非常好。我不确定是什么原因造成的。测试可以顺利通过,但是我在测试运行后抹去了测试之前在数据库中输入的所有内容。

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

这是我的Infrastructure.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));
    }

}

大佬总结

以上是大佬教程为你收集整理的每次运行单元测试时,要清除H2数据库是什么?全部内容,希望文章能够帮你解决每次运行单元测试时,要清除H2数据库是什么?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: