Java DBunit NoSuchTableException

Java DBunit NoSuchTableException,java,spring,unit-testing,dbunit,spring-test-dbunit,Java,Spring,Unit Testing,Dbunit,Spring Test Dbunit,我试图将dbunit和springdbunit包含到我的项目中进行测试。 我有两个文件夹: “src/test/java/dao” 及 “src/test/resources/dao” 在资源中 <?xml version='1.0' encoding='UTF-8'?> <dataset> <Brands id="1" brandName="Apple" /> </dataset> 我的品牌实体: @Entity @Table(name =

我试图将dbunit和springdbunit包含到我的项目中进行测试。 我有两个文件夹: “src/test/java/dao” 及 “src/test/resources/dao”

在资源中

<?xml version='1.0' encoding='UTF-8'?>
<dataset> 
<Brands id="1" brandName="Apple" /> 
</dataset>
我的品牌实体:

@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;
 }
}
@实体
@表(name=“brands”)
大众品牌{
@身份证
@GeneratedValue(策略=GenerationType.AUTO)
私有int-id;
@列(name=“BrandName”,null=false)
私有字符串品牌名称;
@OneToMany(fetch=FetchType.LAZY)
@JoinColumn(name=“BrandId”,updateable=false)
私有列表设备;
公共品牌(){
}
公共品牌(int-id,String-brandName){
this.id=id;
this.brandName=品牌名称;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getBrandName(){
返回此.brandName;
}
公共名称(字符串brandName){
this.brandName=品牌名称;
}   
公共列表getDevices(){
归还此设备;
}
公用设备(列出设备){
这个。设备=设备;
}
公共布尔等于(对象obj){
布尔结果=假;
如果(!(品牌的obj实例))
返回结果;
品牌=(品牌)obj;
if(this.getId()==brand.getId())
结果=真;
返回结果;
}
}
这是我的连接:

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>
bean id=“dataSource”class=“org.apache.commons.dbcp2.BasicDataSource”destroy method=“close”>
org.hibernate.dial.h2方言
真的
UTF-8
创建下降
在使用dbunit之前,出现了相同的问题,并且

<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
createdrop
帮我解决了这个问题。现在,如果我的schema.xml文件为空且 我使用“brandsDao.create(new Brand())”方法在其中一个测试方法中添加品牌, 一切正常。但当我加上

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

对于schema.xml,我得到了这个“NoSuchTableException:Brands”

另外,在将其插入到上下文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>

我有“在模式null中找不到表'BRANDS'。试过不同的文章 这是错误的,但仍然没有任何帮助。我想我的数据库不是在dbunit为 正在尝试从架构中插入数据


如果您能给我一些提示或建议,我们将不胜感激。

谢谢Alan Hay的回复。我已经在SessionFactorybean中尝试了您的hibernate属性集

<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>

${hibernate.dial}
真的
UTF-8
创造
真的
假的
org.hibernate.cache.EhCacheProvider
最有帮助的是

<prop key="hibernate.show_sql">true</prop>
true
这为我提供了虚拟数据库创建的完整跟踪。所有小写的名字, 所以换了

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


成功了。我很确定我以前试过这个,也许我没有包括在内

<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>

当时。尽管如此,它现在运行良好。再次感谢你的提示

现在,我的hibernate属性如下所示:

 <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>

org.hibernate.dial.h2方言
真的
UTF-8
创造

Don;我现在没有时间看,但我这里有一个SpringData项目示例,其中包含基于DBUnit的测试,您可以查看:
<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>