使用Spring+Junit4.4进行测试(使用注解)
来源:互联网 发布:耐克销售额的数据图表 编辑:程序博客网 时间:2024/04/27 20:41
http://nottiansyf.iteye.com/blog/345819
使用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
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
尤其注意用新版的,旧版会出现类未找到的异常
0 0
- 使用Spring+Junit4.4进行测试(使用注解)
- 使用Spring+Junit4.4进行测试(使用注解)
- 使用Spring+Junit4.4进行测试(使用注解)
- 使用Spring+Junit4.4进行测试
- 使用Spring+Junit4.4进行测试
- 使用Spring+Junit4.4进行测试
- JUnit4 使用注解(元注释)进行测试
- JUnit4 使用注解(元注释)进行测试
- Spring项目使用JUnit4进行测试
- 使用JUnit4测试Spring
- 使用JUnit4测试Spring
- 使用JUnit4测试Spring
- Spring整合JUnit4测试使用注解引入多个配置文件
- 使用spring提供的测试功能结合JUnit4进行单元测试
- 【项目实战】使用Spring Test+Junit4整合进行测试
- spring junit4 注解测试
- 使用JUnit4测试Spring(转)
- Spring--IoC--基于注解的DI-使用Spring的JUnit4测试
- Apache/2.4.9 (Win64) PHP/5.5.12 Server at localhost Port 80
- 06数据挖掘原理-数据分析 数据相关性
- where group by having这个顺序问题
- 关于stringstream的一些总结
- 从源码编译构建Jmeter
- 使用Spring+Junit4.4进行测试(使用注解)
- 工业水处理:MSBR威立雅水处理技术主要工艺流程
- HBase Region分裂
- php发送邮件超简单、完美封装
- useradd 和 adduser 区别
- iOS 字符串和数字互相转化
- Jmeter 分布式 Step by Step
- Linux 下zip包的压缩与解压
- Eclipse中如何使用Maven