zoukankan      html  css  js  c++  java
  • spring单元测试

    使用Junit4.4测试

    在类上的配置Annotation

    @RunWith(SpringJUnit4ClassRunner.class) 用于配置spring中测试的环境

    @ContextConfiguration(Locations="../applicationContext.xml") 用于指定配置文件所在的位置

    @Test标注在方法前,表示其是一个测试的方法 无需在其配置文件中额外设置属性.
    多个配置文件时{"/applic","/asas"} 可以导入多个配置文件
    测试中的事务配置 ,

    AbstractTransactionalJUnit38SpringContextTests、 AbstractTransactionalJUnit4SpringContextTests AbstractTransactionalTestNGSpringContextTests 已经在类级别预先配置了好了事物支持
    在普通spring的junit环境中配置事务 在类之前加入注解

    @TransactionConfiguration(transactionManagert="txMgr",defaultRollback=false)

    @Transactional 在方法中主要使用的Annotation包括

    @TestExecutionListeners({})---用于禁用默认的监听器 否着需要通过@contextconfiguration配置一个ApplicationContext;
    @BeforeTransaction

    @Before

    @Rollback(true)

    @AfterTransaction

    @NotTransactional

    package com.d1xn.dao.center.game.imp.test;
    importstatic org.junit.Assert.*;
    import java.util.List;
    import org.junit.Before;

    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;

    @RunWith(SpringJUnit4ClassRunner.
    class)

    //加下面这句的话会调用一个上下文配置文件 文件名为 这个类名-context.xml

    @ContextConfiguration

    public class TestExamDAO {
        @Autowired    

    private IExamDAO dao;    

    @Before    

    publicvoid setUp() throws Exception {    

    }
        @Test    

    public void testGetExamByUserId() {  

                //省略   

    }
    }

    下面是位上面那个类准备的配置文件

    TestExamDao-context.xml

    <!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"><beans>     <import resource="classpath:com/d1xn/dao/center/config/dao-core.xml"/>         <bean id="" class="">             <property name="" ref=""></property>         </bean></beans>
    目前Spring2.5只支持4.4的Junit进行测试 注意需要新的Jar包如下 javassist-3.4.GA.jar hibernate3.jar hibernate-annotations.jar 尤其注意用新版的,旧版会出现类未找到的异常
  • 相关阅读:
    Java实现字符串的包含
    Java实现字符串的包含
    Java实现字符串的包含
    Java实现字符串的包含
    Java实现字符串的包含
    穷文富理撑死工,得先学门能挣钱的手艺
    Windows更新清理工具 (winsxs 清理工具)
    SQLite实现内存键值存储
    Qt5.7.0移植到4412
    罗辑思维2014 第11集 迷茫时代的明白人(慢慢来,能做一点是一点),有书卖
  • 原文地址:https://www.cnblogs.com/shaohz2014/p/3615641.html
Copyright © 2011-2022 走看看