SpringBoot框架下基于Junit的单元测试
来源:互联网 发布:淘宝旺旺怎么改名字 编辑:程序博客网 时间:2024/05/18 06:23
前言
Junit是一个Java语言的单元测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量。是一个在发展,现在已经到junit5,在javaEE开发中与很多框架相集成,使得开发者很方便。
Junit常用注解:
- @Before:初始化方法
- @After:释放资源
- @Test:测试方法,在这里可以测试期望异常和超时时间
- @Ignore:忽略的测试方法
- @BeforeClass:针对所有测试,只执行一次,且必须为static void
- @AfterClass:针对所有测试,只执行一次,且必须为static void
- @RunWith:指定使用的单元测试执行类
Junit测试用例执行顺序:
@BeforeClass ==> @Before ==> @Test ==> @After ==> @AfterClass
过程:就是先加载模拟的环境,再进行测试。
测试准备
依赖版本(不同版本存在一些差异)
- junit 4.12
- spring-test 4.3.6
- spring-boot-test 1.5.1
添加依赖(必须)
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version> 1.5.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version> 4.12</version> </dependency>
编辑器(非必须)
IntellijIDEA
测试代码
测试代码如下:
import cn.yjxxclub.springboot.entity.Member;import cn.yjxxclub.springboot.mapper.MemberMapper;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.boot.test.web.client.TestRestTemplate;import org.springframework.test.context.junit4.SpringRunner;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Author: 遇见小星 * Email: tengxing7452@163.com * Date: 17-6-16 * Time: 下午12:18 * Describe: member应用测试类 */@RunWith(SpringRunner.class)@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)public class MemberTest { /** * Spring RestTemplate的便利替代。你可以获取一个普通的或发送基本HTTP认证(使用用户名和密码)的模板 * 这里不使用 */ @Autowired private TestRestTemplate testRestTemplate; @Autowired MemberMapper memberMapper; /** * 2017-06-16 14:08:09.884 INFO 13803 --- [ main] com.alibaba.druid.pool.DruidDataSource : {dataSource-1} inited size:5 -----测试完毕------- 2017-06-16 14:08:09.955 INFO 13803 --- [ Thread-4] ationConfigEmbeddedWebApplicationContext : Closing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@fd07cbb: startup date [Fri Jun 16 14:08:04 CST 2017]; root of context hierarchy */ @Test public void test(){ Map<String,Object> map = new HashMap(); map.put("start",0); map.put("size",8); List<Member> list = memberMapper.list(map); System.out.println("size:"+list.size()); System.out.println("-----测试完毕-------"); }}
代码说明
- @RunWith 是junit提供的,前言已经说了
- SpringRunner是spring-test提供的测试执行单元类(SpringJUnit4ClassRunner的新名字)
- @SpringBootTest is saying “bootstrap with Spring Boot’s support”,类似springboot程序的测试引导入口
具体请看spring.io解释:
后记
- 在springboot1.4.1以前的版本时候,网上用如下加载方式(这个方式笔者没试过,因为是aliyun的依赖库1.4.1以前的已经不支持了)
@RunWith(SpringRunner.class)@SpringApplicationConfiguration(classes = SpringBootSampleApplication.class)public class MemberTest {
- 在spring的其他项目中一般加载是
@RunWith(SpringRunner.class)@ContextConfiguration(locations={"classpath:spring-servlet.xml", "classpath:spring-dao-test.xml", "classpath:spring-service-test.xml"})public class MemberTest {
- spring-boot-test还提供@DataJpaTest,@JsonTest,@JdbcTest注解,非常方便。
- 不管spring提供的有多方便,还是开始说的那句话:先加载模拟的环境,再进行测试
参考文章
- https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4
- http://www.jianshu.com/p/c0f5545f8ba3
- http://blog.csdn.net/catoop/article/details/50752964
阅读全文
0 0
- SpringBoot框架下基于Junit的单元测试
- SpringBoot的Junit单元测试
- Spring框架下的Junit单元测试
- junit基于注解的单元测试
- 基于spring的junit单元测试
- JUnit单元测试框架的使用
- JUnit单元测试框架的使用
- JUnit单元测试框架的使用
- JUnit单元测试框架的使用
- JUnit单元测试框架的使用
- JUnit单元测试框架的使用
- JUnit单元测试框架的使用
- SpringBoot Junit单元测试
- SpringBoot junit单元测试
- SpringBoot 使用JUnit 单元测试
- Spring框架下基于事务的单元测试
- Android下的junit单元测试
- Android下的junit 单元测试
- AndroidMenifest.xml全解
- python命令行解析工具argparse模块
- Storm集群中的组件介绍
- Fastjson 实体类JSON化过滤字段操作-PropertyFilter
- 逆向工程核心原理学习笔记(十二):分析abex' crackme #1
- SpringBoot框架下基于Junit的单元测试
- CentOS7安装Hadoop2.7.3完整步骤
- Java获取当前操作系统的信息
- kali 源更新
- apiCloud小结
- 逆向工程核心原理学习笔记(十三):分析abex' crackme #1 的延伸:将参数压入栈
- 数据结构实验——查找的有关操作
- java实现后端接口加密
- 逆向工程核心原理学习笔记(十四):栈帧1