前言

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..RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3..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",);
map.put("size",);
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

SpringBoot框架下基于Junit的单元测试的更多相关文章

  1. 在Jena框架下基于MySQL数据库实现本体的存取操作

    在Jena框架下基于MySQL数据库实现本体的存取操作 转自:http://blog.csdn.net/jtz_mpp/article/details/6224311 最近在做一个基于本体的管理系统. ...

  2. 【问题】【SpringBoot】记一次springboot框架下用jackson解析RequestBody失败的问题

    最近项目中遇到了一个问题,费好大劲才发现问题所在,并且修复了问题,下面分享一下这个问题的定位和修复的新路旅程. 先说下背景:该项目用的是SpringBoot框架,主要功能为对外提供一些Restful ...

  3. WPF Prism框架下基于MVVM模式的命令、绑定、事件

    Prism框架下的自定义路由事件和命令绑定 BaseCode XAML代码: <Button x:Class="IM.UI.CommandEx.PrismCommandEx" ...

  4. ThinkPHP框架下基于RBAC的权限控制模式详解

    这几天因为要做一个项目,需要可以对Web应用中通用功能进行封装,其中一个很重要的涉及到了对用户.角色和权限部分的灵活管理.所以基于TP框架自己封装了一个对操作权限和菜单权限进行灵活配置的可控制模式. ...

  5. maven多模块下使用JUnit进行单元测试

    1.选中需要进行测试的service类,右键->new->other->JUnit Test Case,如下图: 2.编写测试代码如下: AppServiceTest.java im ...

  6. 基于junit的单元测试类编写

    首先定义抽象类BaseTest package com.geostar.gfstack.operationcenter.common.util; import com.google.gson.Gson ...

  7. SpringBoot重点详解--使用Junit进行单元测试

    目录 添加依赖与配置 ApplicationContext测试 Environment测试 MockBean测试 Controller测试 情况一 情况二 方法一 方法二 本文将对在Springboo ...

  8. java ssh 框架下 利用junit4 spring-test进行单元测试

    ssh框架下  由于bean实列 都交给spring 管理,要做单元测试就比较苦难,junit4 引入注解方便很多: 1. 加入依赖包 使用Spring的测试框架需要加入以下依赖包: JUnit 4 ...

  9. SSH框架下单元测试的实现

    SSH框架下单元测试的实现 实现的功能 实现了部门的增删改查 对Action进行了单元测试 对Service 进行了单元测试,通过mock的方式实现. 实现的步骤 一.对Action层的单元测试实现 ...

随机推荐

  1. turtle库笔记

    turtle库是学习python的一个重要数据库,在当下是一个很有趣流行的绘制图像的数据库,她把画笔想象为一只小乌龟在爬行,让小乌龟在一个以横轴为x,纵轴为y的画布上行驶,并且会有多样的行驶角度,速度 ...

  2. 怎么在vue中引入layui

    新项目想用layui框架,学习了把前辈是怎么引入layui的,这里记录下 1.index.html要引入layui.js文件 <script src="/static/layui/la ...

  3. sort sorted() reverse() reversed() 的区别

    sort()是可变对象(字典.列表)的方法,无参数,无返回值,sort()会改变可变对象,因此无需返回值.sort()方法是可变对象独有的方法或者属性,而作为不可变对象如元组.字符串是不具有这些方法的 ...

  4. 《大数据日知录》读书笔记-ch1数据分片与路由

    目前主流大数据存储使用横向扩展(scale out)而非传统数据库纵向扩展(scale up)的方式.因此涉及数据分片.数据路由(routing).数据一致性问题 二级映射关系:key-partiti ...

  5. VSCode快捷键整理

    shift+option+command+上下方向键 在上下方向增加或减少光标 shift+option+上下方向件 在复制当前行到下方或上方 位置跟方向键相反 shift+command+k 删除当 ...

  6. Robot Framework自动化测试二(元素定位)

    前言 在学习的过程中,可能会误认为Robot framework 只是个web UI测试工具,更正确的理解Robot framework是个测试框架,之所以可以拿来做web UI层的自动化是国为我们加 ...

  7. 关于window.console&&console.log(123)的思考

    一.JS的且运算记得最开始看到window.console&&console.log(123),当时知道能起什么作用但是没有深入研究,最近在研究后总算弄明白了.要理解这个,首先得明白三 ...

  8. luajit+nginx+上传模块+lua模块编译安装

    git clone https://github.com/fdintino/nginx-upload-module.git git clone https://github.com/openresty ...

  9. centos 同步网络时间

    centos 同步网络时间 # yum -y install ntp ntpdate # ntpdate cn.pool.ntp.org # date

  10. GeneratedKeyHolder的作用:获得新建主键值

    Spring利用GeneratedKeyHolder,提供了一个可以返回新增记录对应主键值的方法: int update(PreparedStatementCreator psc, KeyHolder ...