bboks.net™

Spring Framework을 이용한 HSQLDB 테스트 본문

Java/Java

Spring Framework을 이용한 HSQLDB 테스트

bboks.net 2013. 2. 1. 08:49

※ 본 예제에서는 In-memory DB로 한번 실행이 되면 데이터가 삭제된다.

1. 이클립스에서 Maven Project 생성


2. pom.xml을 수정해서 필요한 Dependency 등록

unit : 4.7

mockito-all : 1.9.5

spring-context : 3.1.2.RELEASE

spring-asm : 3.1.4.RELEASE

spring-beans : 3.1.2.RELEASE

spring-core : 3.1.2.RELEASE

spring-expression : 3.1.2.RELEASE

spring -test : 3.1.2.RELEASE

spring-orm : 3.1.2.RELEASE

hibernate-core : 4.1.9.Final

hsqldb : 2.2.9

cglib : 2.2.2

commons-dbcp : 1.4


3. Entity 클래스 작성

package net.bboks.test.hibernate.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "MEMBERS")
public class Member {
	private int id;
	private String name;
	private double point;

	public Member() {
		
	}
	
	public Member(int id, String name, double point) {
		this.id = id;
		this.name = name;
		this.point = point;
	}

	@Id
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	@Column(length = 100)
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Column(nullable = false)
	public double getPoint() {
		return point;
	}

	public void setPoint(double point) {
		this.point = point;
	}
}


3. Dao 클래스 작성

IMemberDao

package net.bboks.test.hibernate.dao;

import java.util.List;

import org.springframework.transaction.annotation.Transactional;

import net.bboks.test.hibernate.entity.Member;

@Transactional
public interface IMemberDao {
	public List list();
	
	public Member get(int id);
	
	public void save(Object obj);
	
	public void delete(Object obj);
}


MemberDao

package net.bboks.test.hibernate.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import net.bboks.test.hibernate.entity.Member;

public class MemberDao implements IMemberDao {

	@Autowired
	private SessionFactory sessionFactory;
	
	private Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@SuppressWarnings("unchecked")
	public List list() {
		return getCurrentSession().createCriteria(Member.class).list();
	}

	public Member get(int id) {
		return (Member)getCurrentSession().get(Member.class, id);
	}

	public void save(Object obj) {
		getCurrentSession().save(obj);
	}

	public void delete(Object obj) {
		getCurrentSession().delete(obj);
	}
}


4. HSQLDB 구동시 실행할 스크립트 작성

create table members (
    id        decimal,
    name    varchar(100),
    point    decimal
);

insert into members (id, name, point) values(1, 'a', 1.00);
insert into members (id, name, point) values(2, 'b', 2.00);
insert into members (id, name, point) values(3, 'c', 3.00);
insert into members (id, name, point) values(4, 'd', 4.00);
insert into members (id, name, point) values(5, 'e', 5.00);
insert into members (id, name, point) values(6, 'f', 6.00);
insert into members (id, name, point) values(7, 'g', 7.00);
insert into members (id, name, point) values(8, 'h', 8.00);
insert into members (id, name, point) values(9, 'i', 9.00);
insert into members (id, name, point) values(10, 'j', 10.00);
insert into members (id, name, point) values(11, 'k', 11.00);
insert into members (id, name, point) values(12, 'l', 12.00);
insert into members (id, name, point) values(13, 'm', 13.00);
insert into members (id, name, point) values(14, 'n', 14.00);
insert into members (id, name, point) values(15, 'o', 15.00);
commit;


5. 테스트 시 사용할 Context 작성

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:annotation-config />

    <!-- DataSource로 HSQL 사용 설정 -->
    <jdbc:embedded-database id="dataSource" type="HSQL">
        <!-- HSQLDB 구동시 실행할 스크립스 설정 -->
        <jdbc:script location="classpath:/META-INF/script-create.sql"/>
    </jdbc:embedded-database>

    <bean id="sessionFactory" scope="singleton" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="net.bboks.test.hibernate.entity" />
       
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.HSQLDialect</prop>
                <prop key="hibernate.cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.pool_size">1</prop>
            </props>
        </property>
    </bean>

    <bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

    <bean id="transactionManager" scope="singleton"
        class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>
   
    <bean class="net.bboks.test.hibernate.dao.MemberDao" />
   
</beans>


6. Test Case 작성

package net.bboks.test.hibernate;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;

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.transaction.annotation.Transactional;

import net.bboks.test.hibernate.entity.Member;
import net.bboks.test.hibernate.dao.IMemberDao;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/META-INF/member_context.xml" })
public class MemberTest {
	@Autowired
	IMemberDao memberDao;

	@Test
	@Transactional
	public void testList() {

		for (Member m : memberDao.list()) {
			System.out.println(m.getName());
		}
	}

	@Test
	@Transactional
	public void testGet() {
		Member member = memberDao.get(1);
		
		assertThat(member.getId(), is(1));
	}

	@Test
	@Transactional
	public void testSave() {
		Member member = new Member(16, "hwanbok", 100.00);
		
		memberDao.save(member);
		
		member = memberDao.get(16);
		
		assertThat(member.getId(), is(16));
		assertThat(member.getName(), is("hwanbok"));
		assertThat(member.getPoint(), is(100.00));
	}

	@Test(expected = NullPointerException.class)
	@Transactional
	public void testDelete() {
		Member member = new Member(1, "a", 1.00);
		
		memberDao.delete(member);
		
		member = memberDao.get(1);
		
		assertThat(member, null);
	}
}


7. 테스트 결과


8. 최종적으로 구성된 프로젝트 구조는 다음과 같다.