使用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 
 
Junit4.4下支持类,方便基于junit4.4的测试 
AbstractJUnit4SpringContextTests: 
 
AbstractTransactionalJUnit4SpringContextTests: 
需要在applicationContext中定义一个datasource 
 
2009年3月9日 
目前Spring2.5只支持4.4的Junit进行测试 
下面是一个简单的测试Demo 
 
 
 1 package com.gameplus.service.webService;
 2 
 3 import javax.annotation.Resource;
 4 
 5 import org.junit.Test;
 6 import org.junit.runner.RunWith;
 7 import org.springframework.test.context.ContextConfiguration;
 8 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 9 
10 @RunWith(SpringJUnit4ClassRunner.class)
11 @ContextConfiguration(locations={"../../../../applicationContext.xml","../../../../applicationDatasource.xml"})
12 public class UserServiceTest  {
13     @Resource
14     private IUserService userService;
15     
16     @Test
17     public void testAddOpinion1() {
18         userService.downloadCount(1);
19         System.out.println(1);
20     }
21     @Test
22     public void testAddOpinion2() {
23         userService.downloadCount(2);
24         System.out.println(2);
25     }
26 }
27 

注意需要新的Jar包如下 
javassist-3.4.GA.jar 
hibernate3.jar 
hibernate-annotations.jar 
尤其注意用新版的,旧版会出现类未找到的异常

Junit4测试Spring的更多相关文章

  1. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  2. 使用JUnit4测试Spring

    测试DAO import static org.junit.Assert.*; import org.junit.Before; import org.junit.Ignore; import org ...

  3. junit4测试 Spring MVC注解方式

    本人使用的为junit4进行测试 spring-servlet.xml中使用的为注解扫描的方式 <?xml version="1.0" encoding="UTF- ...

  4. 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers

    就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/43 ...

  5. JUnit4 测试示例

    1. JUnit4 测试示例 // Calculator.java public class Calculator{ public int add(int a, int b){ return a + ...

  6. (转)编写Spring的第一个案例并测试Spring的开发环境

    http://blog.csdn.net/yerenyuan_pku/article/details/52832145 Spring4.2.5的开发环境搭建好了之后,我们来编写Spring的第一个案例 ...

  7. Junit4 测试代码

    Junit4 测试代码 import org.junit.Test; import org.junit.runner.RunWith; @RunWith(SpringJUnit4ClassRunner ...

  8. 利用Spring的junit4测试

    利用Spring的JUnit4进行测试 不需要再显式创建Spring容器和getBean @RunWith(SpringJUnit4ClassRunner.class) @ContextConfigu ...

  9. spring中试用junit4测试

    一:加入jar包 <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId& ...

随机推荐

  1. 非负矩阵分解(4):NMF算法和聚类算法的联系与区别

    作者:桂. 时间:2017-04-14   06:22:26 链接:http://www.cnblogs.com/xingshansi/p/6685811.html 声明:欢迎被转载,不过记得注明出处 ...

  2. 面试题 ARC

    什么是ARC ?ARC主要解决什么问题? ARC:自动引用计数. 要点..当对象被创建时 retain count+1, 当对象被release时 retain count-1, 当retain co ...

  3. IOS 程序运行过程

    第一次写有点小紧张  希望大家多多指教! 主要讲讲程序从点击运行到结束这个过程中后面的代码都有哪些变化. 首先先了解一下UIApplication.UIApplication的核心作用是提供IOS运行 ...

  4. 实时监听input标签输入 实时监听文本框输入 避免中文输入法无法触发onkeyup事件的问题

    前言: 对于实时监听输入,这种需求大多数都是用于一个联想字提醒,智能提醒.大家都知道onkeydown,onkeypress,onkeyup的在监听中文输入法或者右键粘贴的时候都存在一些弊端,不是那么 ...

  5. 基于Prism.Windows的UWP开发备忘

    以前做UWP开发都是使用MvvmLight,主要是简单易上手,同时也写了很多MvvmLight的开发系列文章: UWP开发必备以及常用知识点总结 UWP开发之Mvvmlight实践九:基于MVVM的项 ...

  6. java异常处理机制(try-catch-finally)

    /* * 异常处理机制 * 1.分类:Error和Exception * Error错误是JVM自动报错的,程序员无法解决例如开数组过大int a[]=new int [1024*1024*1024] ...

  7. 使用charles抓取htpps的方法

    自己整理的步骤做个记录 1.下载证书,官方地址:http://www.charlesproxy.com/ssl.zip 可直接点击链接下载:http://charlesproxy.com/getssl ...

  8. PHP 学习笔记(2)

    <?php$foo = "0";  // $foo 是字符串 (ASCII 48)$foo += 2;   // $foo 现在是一个整数 (2)$foo = $foo +  ...

  9. PHP 手册

    http://www.php.net/manual/zh/index.php 感谢中文翻译工作者. PHP 手册¶ by:Mehdi Achour Friedhelm Betz Antony Dovg ...

  10. python循环

    #!/usr/bin/python #coding:utf-8 lcf_age = "19" count = 0 while count < 3: c_lcf_age = i ...