Energy of Love  
日历
<2011年11月>
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910
统计
  • 随笔 - 70
  • 文章 - 0
  • 评论 - 80
  • 引用 - 0

导航

常用链接

留言簿

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
使用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 
尤其注意用新版的,旧版会出现类未找到的异常 
posted on 2011-11-30 19:38 不高兴 阅读(54700) 评论(3)  编辑  收藏 所属分类: Java
评论:

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 不高兴 Powered by: 博客园 模板提供:沪江博客