gpt4 book ai didi

java - DBunit NoSuchTableException

转载 作者:行者123 更新时间:2023-12-01 12:32:40 35 4
gpt4 key购买 nike

我正在尝试将 dbunit 和 spring dbunit 包含到我的项目中进行测试。我有 2 个文件夹:“src/测试/java/dao”和“src/test/resources/dao”。

在资源dao中

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

尝试过大写和小写(BRANDS、品牌),仍然有同样的问题。

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

我有“在架构 null 中未找到表‘BRANDS’”。尝试了不同的文章这个错误,但仍然没有任何帮助。我猜当 dbunit 是时我的数据库没有创建尝试从架构插入数据。

将不胜感激一些提示或建议。

最佳答案

感谢艾伦·海伊的回复。我已经在我的 sessionFactory bean 中尝试了您的 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>

最有帮助的是

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

它为我提供了虚拟数据库创建的完整跟踪。所有名字都是小写的,所以改变后

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

当时。不过现在它工作得很好。再次感谢您的提示。

现在我的 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>

关于java - DBunit NoSuchTableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25821235/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com