2013-04-08 101 views
1

我有一個Spring + Hibernate + H2項目,這個項目是我在互聯網上找到的一個例子。除了每次我運行一個單元測試時,db都會被擦除。我不確定是什麼原因造成的。測試通過的很好,但是在測試運行之後,我在測試之前放入db的任何東西都會被清除掉。每次運行單元測試時,我的H2數據庫都在擦什麼?

任何想法都會有幫助!謝謝!

這裏是我的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", "[email protected]", "Tom", "Hanks", "Itsasecret", "CA","54221"); 
     patrick = new User("847 Mapple Dr.", "", "Washington", "[email protected]", "Patrick", "Steward", "moneyMonkey", "MD","64541"); 
     john = new User("8484 Bristol", "", "Columbus", "[email protected]", "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)); 
    } 

} 

回答

6

這是屬性名稱=「hibernate.hbm2ddl.auto」value =「create」/> 這是每次下降n重新創建模式。 將其更改爲更新,因此只會在第一次創建時纔會更新。

請參閱此鏈接更多的描述: Link

+0

我改成了更新喜歡你的建議,並停止結算數據庫。但是,我堅持的實體並沒有在測試結束後進入db。我採用了相同的代碼,並使用main方法在單獨的類中運行它,並顯示了實體。我猜測@RunWith(SpringJUnit4ClassRunner.class)強制要在測試結束時回滾數據庫。謝謝你的幫助! – HappyCoder86 2013-04-08 04:08:18

相關問題