I'm trying to include dbunit and spring dbunit to my project for testing. I have 2 folders: "src/test/java/dao" and "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.
In main 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
<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. But when I add
<Brands id="1" brandName="Apple" />
to my schema.xml, I get this "NoSuchTableException: Brands".
Also, before I've inserted this to my context 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". 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.
Will be grateful for some hints or advices.