there is a property in hibernate that allows you to specify an sql script to run when the session factory is initialized. with this, i can now populate tables with data that required by my dao layer. the property is as follows;
<prop key="hibernate.hbm2ddl.import_files">import.sql</prop>
let me first show you the dao class i am going to test;
package com.unittest.session.example1.dao;
import org.springframework.transaction.annotation.propagation;
import org.springframework.transaction.annotation.transactional;
import com.unittest.session.example1.domain.employee;
@transactional(propagation = propagation.required)
public interface employeedao {
public long createemployee(employee emp);
public employee getemployeebyid(long id);
}
nothing major, just a simple dao with two methods where one is to persist and one is to retrieve. for me to test the retrieval method i need to populate the employee table with some data. this is where the import sql script which was explained before comes into play. the import.sql file is as follows;
insert into employee (empid,emp_name) values (1,'emp test');
next up let us see how easy it is to run integration tests with spring.
package com.unittest.session.example1.dao.hibernate;
import static org.junit.assert.*;
import org.junit.test;
import org.junit.runner.runwith;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.test.context.contextconfiguration;
import org.springframework.test.context.junit4.springjunit4classrunner;
import org.springframework.test.context.transaction.transactionconfiguration;
import com.unittest.session.example1.dao.employeedao;
@runwith(springjunit4classrunner.class)
@contextconfiguration(locations="classpath:spring-context.xml")
@transactionconfiguration(defaultrollback=true,transactionmanager="transactionmanager")
public class employeehibernatedaoimpltest {
@autowired
private employeedao employeedao;
@test
public void testgetemployeebyid() {
employee emp = employeedao.getemployeebyid(1l);
assertnotnull(emp);
}
@test
public void testcreateemployee()
{
employee emp = new employee();
emp.setname("emp123");
long key = employeedao.createemployee(emp);
assertequals(2l, key.longvalue());
and finally i present the spring configuration which wires everything up;
<?xml version="1.0" encoding="utf-8"?>
xsi:schemalocation="
<context:component-scan base-package="com.unittest.session.example1" />
<context:annotation-config />
<tx:annotation-driven />
<bean id="sessionfactory"
class="org.springframework.orm.hibernate3.annotation.annotationsessionfactorybean">
<property name="packagestoscan">
<list>
<value>com.unittest.session.example1.**.*</value>
</list>
</property>
<property name="hibernateproperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.mysqldialect</prop>
<prop key="hibernate.connection.driver_class">com.mysql.jdbc.driver</prop>
<prop key="hibernate.connection.url">jdbc:mysql://localhost:3306/hbmex1</prop>
<prop key="hibernate.connection.username">root</prop>
<prop key="hibernate.connection.password">password</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- -->
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.hbm2ddl.import_files">import.sql</prop>
</props>
</bean>
<bean id="empdao"
class="com.unittest.session.example1.dao.hibernate.employeehibernatedaoimpl">
<property name="sessionfactory" ref="sessionfactory" />
<bean id="transactionmanager"
class="org.springframework.orm.hibernate3.hibernatetransactionmanager">
</beans>