2014-09-13 64 views
0

我試圖將dbunit和spring dbunit包含到我的項目中進行測試。 我有2個文件夾: 「src/test/java/dao」 和 「src/test/resources/dao」。DBunit NoSuchTableException

在資源DAO

<?xml version='1.0' encoding='UTF-8'?> 
<dataset> 
<Brands id="1" brandName="Apple" /> 
</dataset> 

用大寫和小寫試過(品牌,品牌),仍然具有同樣的問題。

在主要的Java

public class BrandsDaoTest { 

@Autowired 
private BrandsDao brandsDao; 
private Brand brand; 

private static final int ID = 1; 
private static final String BRANDNAME = "apple"; 
private static final String UBRANDNAME = "horizont"; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({ "classpath:app-context.xml","classpath:test-context.xml" }) 
@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, 
DbUnitTestExecutionListener.class }) 
@Test 
@DatabaseSetup("schema.xml") 
public void find(){ 

    Brand brand = brandsDao.find(1); 
    assertNotNull(brand); 
    brandsDao.delete(ID); 
    assertNull(brandsDao.find(ID)); 

} 
} 

我的品牌實體:

@Entity 
@Table(name = "brands") 
public class Brand { 

@Id 
@GeneratedValue(strategy=GenerationType.AUTO) 
private int id; 


@Column(name="BrandName",nullable=false) 
private String brandName; 
@OneToMany(fetch = FetchType.LAZY) 
@JoinColumn(name="BrandId",updatable=false) 
private List<Device> devices; 


public Brand(){ 

} 

public Brand(int id,String brandName){ 
    this.id = id; 
    this.brandName = brandName; 
} 

public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 


public String getBrandName() { 
    return this.brandName; 
} 
public void setBrandName(String brandName) { 
    this.brandName = brandName; 
} 


public List<Device> getDevices(){ 
    return this.devices; 
} 

public void setDevices(List<Device> devices){ 
    this.devices = devices; 
} 

public boolean equals(Object obj) { 
    boolean result = false; 
     if (!(obj instanceof Brand)) 
      return result; 
     Brand brand = (Brand)obj; 
     if(this.getId() == brand.getId()) 
      result = true; 
     return result; 
} 
} 

這是我的聯繫:

bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="org.h2.Driver"/> 
    <property name="url"  value="jdbc:h2:mem:dbtest; 
    MODE=MySQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE" /> 

    <property name="username" value="sa"/> 
    <property name="password" value=""/> 
</bean> 

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" 
     value="com.entities" /> 

    <property name="hibernateProperties"> 
     <props> 
      <prop key="dialect">org.hibernate.dialect.H2Dialect</prop> 
      <prop key="connection.useUnicode">true</prop> 
      <prop key="connection.characterEncoding">UTF-8</prop> 
      <prop key="hibernate.hbm2ddl.auto">create-drop</prop> 
     </props> 
    </property> 
</bean> 

使用的DbUnit之前,有同樣的問題,並

<prop key="hibernate.hbm2ddl.auto">create-drop</prop> 

爲我解決了這個問題。現在,如果我的schema.xml文件爲空,並且我使用「brandsDao.create(new Brand())」方法在其中一種測試方法中添加品牌,則 一切正常。但當我添加

<Brands id="1" brandName="Apple" /> 

我的schema.xml,我得到這個「NoSuchTableException:品牌」。

而且,我已經插入這我的上下文XML之前:

<bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean"> 
    <property name="datatypeFactory"> 
     <bean class="org.dbunit.ext.mysql.MySqlDataTypeFactory" /> 
    </property> 
    <property name="caseSensitiveTableNames" value="true" /> 
</bean> 

<bean id="dbUnitDatabaseConnection" class="com.github.springtestdbunit.bean. 
DatabaseDataSourceConnectionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="databaseConfig" ref="dbUnitDatabaseConfig"/> 

</bean> 

我有「沒有發現架構空表的品牌'」。嘗試了 這個錯誤的不同文章,但仍然沒有什麼幫助。我想我的數據庫不是在dbunit是 試圖從模式插入數據時創建的。

將不勝感激的一些提示或建議。

+0

唐;噸有時間看的時刻,但我在這裏與基於DBUnit的測試樣本SpringData項目,你可以看看:https://開頭github.com/alanhay/spring-data-jpa-bootstrap – 2014-09-13 08:32:26

回答

1

感謝Alan Hay的回覆。我試過你的Hibernate屬性設置爲我的sessionFactory豆

<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="packagesToScan" 
     value="com.entities" /> 

    <property name="hibernateProperties"> 
     <props> 
      <prop key="dialect">${hibernate.dialect}</prop> 
      <prop key="connection.useUnicode">true</prop> 
      <prop key="connection.characterEncoding">UTF-8</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
      <prop key="hibernate.show_sql">true</prop> 
      <prop key="hibernate.cache.use_second_level_cache">false</prop> 
      <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop> 
     </props> 
    </property> 
</bean> 

,什麼是最有幫助的是

<prop key="hibernate.show_sql">true</prop> 

與我的虛擬的DB創建的完整的跟蹤提供了我。所有的名字,其中有小寫,改變

<Brands id="1" brandName="Apple" /> 

<brands id="1" brandName="Apple" /> 
<brands id="2" brandName="Apple2" /> 

後 所以它的工作。我很確定我以前試過這個,也許我沒有包括

<bean id="dbUnitDatabaseConfig" class="com.github.springtestdbunit.bean.DatabaseConfigBean"> 
    <property name="datatypeFactory"> 
     <bean class="org.dbunit.ext.mysql.MySqlDataTypeFactory" /> 
    </property> 
    <property name="caseSensitiveTableNames" value="true" /> 
</bean> 

在那個時候。不過現在它工作正常。再次感謝提示。

現在我的休眠特性是這樣的:

<props> 
      <prop key="dialect">org.hibernate.dialect.H2Dialect</prop> 
      <prop key="connection.useUnicode">true</prop> 
      <prop key="connection.characterEncoding">UTF-8</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
      <!--<prop key="hibernate.show_sql">true</prop> --> 

     </props>