简体   繁体   English

DBunit NoSuchTableException

[英]DBunit NoSuchTableException

I'm trying to include dbunit and spring dbunit to my project for testing. 我正在尝试将dbunit和spring dbunit包含到我的项目中进行测试。 I have 2 folders: "src/test/java/dao" and "src/test/resources/dao". 我有2个文件夹:“ src / test / java / dao”和“ src / test / resources / dao”。

In resources dao 在资源岛

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

Tried with uppercase and lower case(BRANDS, brands), still having same problem. 尝试使用大写和小写(BRANDS,品牌),仍然存在相同的问题。

In main java 在主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));

}
}

My brand entity: 我的品牌实体:

@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;
 }
}

This is my connection: 这是我的联系:

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>

Before using dbunit, had the same problem, and 在使用dbunit之前,有同样的问题,并且

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

solved the problem for me. 为我解决了问题。 Now if my schema.xml file is empty and I'm adding brands in one of the test methods with "brandsDao.create( new Brand() )" method, everything works fine. 现在,如果我的schema.xml文件为空,并且要使用“ brandsDao.create(new Brand())”方法在一种测试方法中添加品牌,则一切正常。 But when I add 但是当我添加

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

to my schema.xml, I get this "NoSuchTableException: Brands". 到我的schema.xml,我得到这个“ NoSuchTableException:品牌”。

Also, before I've inserted this to my context xml: 另外,在将其插入到上下文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>

I had "Did not find table 'BRANDS' in schema null". 我有“在模式null中找不到表'BRANDS'”。 Tried different articles on this errors, but still nothing helped. 尝试过有关此错误的其他文章,但仍然无济于事。 I guess my database is not created when dbunit is trying to insert data from schema. 我猜我在dbunit尝试从架构插入数据时未创建数据库。

Will be grateful for some hints or advices. 对于某些提示或建议将不胜感激。

Thanks Alan Hay for your reply. 感谢Alan Hay的回复。 I've tried your set of hibernate properties to my sessionFactory bean 我已经将您的休眠属性集尝试到了sessionFactory 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">${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>

And what was most helpfull is 最有帮助的是

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

which provided me with full trace of my virtual db creation. 这为我提供了完整的虚拟数据库创建跟踪。 All names where lowercase there, so after changing 所有名称都在那里小写,所以更改后

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

to

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

It worked. 有效。 I'm pretty sure I've tried this before, maybe I didn't include 我敢肯定,我之前曾经尝试过,也许我没有

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

at that time. 那时候。 Nevertheless it works fine now. 但是,现在它可以正常工作。 Thanks for hints again. 再次感谢您的提示。

Now my hibernate properties look like: 现在,我的休眠属性如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM