spring,Hibernate,Junit整合

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;
import com.unittest.session.example1.domain.Employee;

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

}

import.sql

insert into Employee (empId,emp_name) values (1,'Emp testa');
spring-context.xml

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="  
          http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd  
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd  
          http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd  
          http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">


	<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/test</prop>
				<prop key="hibernate.connection.username">root</prop>
				<prop key="hibernate.connection.password"></prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- -->
				<prop key="hibernate.hbm2ddl.auto">create</prop>
				<prop key="hibernate.hbm2ddl.import_files">import.sql</prop>
			</props>
		</property>
	</bean>

	<bean id="empDAO"
		class="com.unittest.session.example1.dao.hibernate.EmployeeHibernateDAOImpl">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

</beans>

CREATE TABLE employee(
  empId INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  emp_name VARCHAR(1024) NOT NULL
);
原文: http://www.javacodegeeks.com/2012/11/how-cool-is-integration-testing-with-spring-and-hibernate.html

源代码:http://pan.baidu.com/share/link?shareid=741187651&uk=3878681452



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值