前言

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. dp--hdu1171(01背包)

    hdu1171 题目 Problem Description Nowadays, we all know that Computer College is the biggest department ...

  2. hive Getting Started

    Apache HiveThe Apache Hive™ data warehouse software facilitates reading, writing, and managing large ...

  3. VUE安装步骤

    项目构建 项目推荐直接使用 Vue 官方提供的脚手架(Vue-cli),所以第一步首先是安装脚手架.在命令行或者 IDE 的 Terminal 窗口中输入以下命令即可自动安装: npm install ...

  4. 3dsmax2013卸载/安装失败/如何彻底卸载清除干净3dsmax2013注册表和文件的方法

    3dsmax2013提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装3dsmax2013失败提示3dsmax2013安装未完成,某些产品无法安装,也有时候想重新 ...

  5. 为什么要实现Serializable

    工作中我们经常在进行持久化操作和返回数据时都会使用到javabean来统一封装参数,方便操作,一般我们也都会实现Serializable接口,那么问题来了,首先:为什么要进行序列化:其次:每个实体be ...

  6. how to run windows programs on a MAC?

    How to run windows programs on a MAC? We could use wine or Wine Bottler which is based on wine and p ...

  7. 【ExtJS】关于Component生命周期

    很久以前就学习过extjs的组件生命周期,很久之后,再回头看一看,又增加好多新的认识. extjs组件生命周期大体分为3个阶段:初始化.渲染.销毁. 第一阶段:初始化 初始化工作开始于组件的诞生,所有 ...

  8. 使用SubstanceDesign和Unity插件ShaderForge制作风格化火焰

    使用 SubstanceDesign 软件可以制作shader用的特殊图片,原来真有这种软件,一直好奇这种图片怎么做的 https://www.kancloud.cn/hazukiaoi/sd_sf_ ...

  9. Django级联删除的选项

    Django级联删除的选项 Django模型中的on_delete属性具有如下选项: CASCADE 级联删除,也就是被引用的实体被删除后,相关的记录信息都会被删除. PROTECT 阻止删除被引用的 ...

  10. <数据挖掘导论>读书笔记2

    1.频率和众数 frequency(vi)=具有属性值vi的对象数/m 分类属性的众数mode是具有最高频率的值. 2.百分位数 3.位置度量:均值和中位数 4.散布度量:极差和方差 绝对平均偏差 A ...