2016-11-16 40 views
0

我試圖測試一個使用SpringMVC和Hibernate與MySQL的應用程序。我試過使用HSQLDB,但由於語法與MySQL不一樣,所以查詢可能不起作用,所以我決定去H2。 問題是,當我運行它說「表MAILCONFIG未找到」,並且如果我在INIT語法創建它說已存在。休眠不是在內存數據庫中創建H2的表格

我已經配置了一個不做任何事情的簡單測試,我只希望它運行。

我有以下文件:

ServiceTest.java

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = {"file:src/test/resources/applicationContext.xml"}) 
@Transactional 
@WebAppConfiguration 
public class ServiceTest { 

private static DataSource ds; 

@BeforeClass 
public static void setUpConnection(){ 
    ds = new DataSource(); 
    ds.setDriverClassName("org.h2.Driver"); 
    ds.setUrl("jdbc:h2:mem:testDB"); 
    ds.setUsername("sa"); 
    ds.setPassword(""); 
    HibernateConfiguration.dataSourceTest(ds); 
} 

@AfterClass 
public static void cleanConnection(){ 
    HibernateConfiguration.dataSourceTest(null); 
} 
} 

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd 
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

<bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="persistenceXmlLocation" value="classpath:persistence.xml" /> 
    <property name="persistenceUnitName" value="testingSetup" /> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
    </property> 
</bean> 

<context:component-scan base-package="com.adistec" /> 
<context:annotation-config/> 

的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_2_0.xsd" 
     version="2.0"> 
<persistence-unit name="testingSetup" transaction-type="RESOURCE_LOCAL"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <properties> 
     <property name="hibernate.connection.driver_class" value="org.h2.Driver" /> 
     <property name="hibernate.connection.url" value="jdbc:h2:mem:testDB;DB_CLOSE_DELAY=-1;MODE=MySQL" /> 
     <property name="hibernate.connection.username" value="sa" /> 
     <property name="hibernate.connection.password" value="" /> 
     <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> 
     <property name="hibernate.archive.autodetection" value="class, hbm"/> 
     <property name="hibernate.show_sql" value="false"/> 
     <property name="hibernate.hbm2ddl.auto" value="create"/> 
    </properties> 
</persistence-unit> 

MailConfig.java

@Entity 
public class MailConfig { 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    Long id; 
    @Column 
    String host; 
} 

我在測試中指定的Hibernate配置,因爲應用程序使用的Java類,而不是XML文件。

任何人都可以幫助我嗎?或者,如果有更簡單的方法來測試它與Java類也是受歡迎的,我還找不到解決方案。 我希望它在本地工作,然後它也必須與詹金斯一起工作,因此它可以創建在虛擬機上無法做到的事情。

謝謝!

回答