Spring + JUNIT4 + JPA/Hibernate + H2 集成测试

在实际开发测试中(这里指带有hibernate或其它orm工具的集成开发),如果数据库服务器连接不上了,怎么办?你可能会想到重新在自己的机器上安装数据库,实际上还有更好的方法:使用更小更方快捷的h2数据库,建库建表自动完成,单元测试,集成测试都很方便

版本环境
spring:3.1.1.RELEASE
spring-test:3.1.1.RELEASE
hibernate.version:3.5.6-Final
junit:4.8

1.Spring 配置文件applicationContext.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:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
   <jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS" >
        <jdbc:script location="classpath:db-schema.sql"/>
        <jdbc:script location="classpath:db-test-data.sql"/>
    </jdbc:initialize-database>

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
          destroy-method="close">
        <property name="driverClassName" value="org.h2.Driver"/>
        <property name="url" value="jdbc:h2:file://D:\h2\testdb;USER=SA;"/>
    </bean>


    <!-- 使用 annotation 自动注册bean,并检查@Controller, @Service, @Repository注解已被注入 -->
    <context:component-scan base-package="domain,dao"/>

    <!-- Hibernate SessionFactory -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">

        <property name="dataSource" ref="dataSource"></property>
        <property name="packagesToScan">
            <list>
                <!--sessionFactory的packagesToScan指定了需要扫描的包,只有在指定包下的类使用的注解才有效-->
                <value>domain</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>

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

            </props>
        </property>
    </bean>

    <!-- 配置事务管理 -->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <!-- 配置注解实现管理事务(cglib:proxy-target-class="true") -->
    <tx:annotation-driven transaction-manager="transactionManager"
                          proxy-target-class="true"/>

    <!--这是事务定义,而且是使用注解方式定义事务(@Transactional),proxy-target-class="true"表示采用动态代理类来管理事务,
</beans>

2.db-schema.sql和db-test-data.sql文件内容

 

#db-schema.sql文件
 
DROP TABLE IF EXISTS USERSE;
CREATE TABLE USERSE(ID INT PRIMARY KEY, NAME VARCHAR(255));

#db-test-data.sql

INSERT INTO USERSE VALUES(1, 'Hello');

 

3.单元测试类

@RunWith(SpringJUnit4ClassRunner.class)
// specifies the Spring configuration to load for this test fixture
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class UserDAOImplTest {
    @Autowired
    private UserDAO userDAO;
    @Test
    public void testGetList() throws Exception {
        List dlist = userDAO.getList();
        System.out.println("==========="+dlist.size());
        System.in.read();
    }
}

转载于:https://my.oschina.net/88sys/blog/77462

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值