Spring+junit4 实现注解测试原理解析。
来源:互联网 发布:mac和手机 日历同步 编辑:程序博客网 时间:2024/06/05 22:59
背景:
我们在使用Spring集成的项目进行开发的时候,需要在不依赖web容器启动的时候去进行单元测试,而Spring为我们提供了相应单元测试框架,在spring 的org.springframework.test.context包内,我们只要在单元测试中引入相应的注解,就可以轻松的实现单元测试。其实好的单元测试可以大大提高我们的生产力,加快我们的开发速度。
junit单元测试优势:1、不用在每个类里面都写main方法,然后去测试。
2、可以得到每个方法执行所消耗的 时间,不用自己计算。
junit单元测试环境搭建:
spring集成Junit4需要两个jar包:junit-4.10.jar和spring-test-4.2.0.RELEASE.jar。
使用方法
下图是我自己写的一个测试类的方法。
使用很简单
1)加入Junit4的注解 @RunWith,在这里可以指定Spring的的运行器来集成。
2)加入@ContextConfiguration注解,指定要加载的配置文件的位置。
实例:
package com.mqAction;import java.util.Date;import java.util.List;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;import com.common.common.entity.SmsRecord;import com.common.common.service.ISmsRecordService;@RunWith(SpringJUnit4ClassRunner.class) //使用junit4进行测试 @ContextConfiguration(locations={"classpath*:spring-mybatis.xml"}) public class FilterMain {@Autowiredprivate ISmsRecordService smsRecordService;//判断数据插入是否成功,并且返回插入主键编号@Testpublic void test(){SmsRecord smsRecord =new SmsRecord();smsRecord.setAppType("wlsq");smsRecord.setCreateTime(new Date());smsRecord.setEntranceType(1);smsRecord.setIdentification(0);smsRecord.setJim("6e0fc0fddd294862a51764bf67a151ee");smsRecord.setMessageId("ox11102202");smsRecord.setPushType("wlsq");smsRecord.setTelephone("13265740591");int id = smsRecordService.insert(smsRecord);System.out.println("返回主键Id:"+smsRecord.getId());}//数据查询(通过)@Testpublic void test1(){SmsRecord smsRecord =new SmsRecord();smsRecord.setId(231);List<SmsRecord> list= smsRecordService.selectByObject(smsRecord);System.out.println("返回主键Id:"+list.get(0).getMessageId());}//数据更改操作@Testpublic void test2(){SmsRecord smsRecord =new SmsRecord();smsRecord.setId(237);smsRecord.setIdentification(1);smsRecord.setUpTime(new Date());int result = smsRecordService.updateByPrimaryKeySelective(smsRecord);System.out.println("更新数据数量:"+result);}}
0 0
- Spring+junit4 实现注解测试原理解析。
- Spring+junit4 实现注解测试原理解析
- spring junit4 注解测试
- Spring+junit4单元测试原理解析
- spring注解原理解析
- 浅谈Spring测试框架+junit4单元测试原理
- JUnit4注解测试SSH
- Junit4中的测试注解
- spring相关JUnit4注解
- Spring注解实现解析
- 使用Spring+Junit4.4进行测试(使用注解)
- 使用Spring+Junit4.4进行测试(使用注解)
- 使用Spring+Junit4.4进行测试(使用注解)
- Spring整合JUnit4测试使用注解引入多个配置文件
- junit4测试service-spring
- 使用JUnit4测试Spring
- 使用JUnit4测试Spring
- Spring整合Junit4测试
- Android Activity使用Dialog样式导致点击空白处自动关闭的问题解决
- JavaScript之表达式和运算符
- C++中用Socket实现对结构体、长字符串和图片的传输
- 29912
- ConnectionString密码丢失的解决方法
- Spring+junit4 实现注解测试原理解析。
- socket 连接域名
- 安卓中常见的异常
- redhat操作命令笔记---系列3
- Android中Bundle的用法
- idea svn 切换分支
- 洲新城 IT教育 李赞红老师 第一章节
- 观察者
- Java多线程之wait(),notify(),notifyAll()