添加依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>

新建测试类

在web项目(即含有SpringApplication启动类)中test目录新建测试类, 包路径和SpringApplication中的扫描路径一致,否则无法启动测试类。若测试类的包路径和启动类的包路径不一致,会出现以下错误信息:

Neither @ContextConfiguration nor @ContextHierarchy found for test class [xx.xx.Test], using SpringBootContextLoader

Could not detect default resource locations for test class [xx.xx.Test]: no resource found for suffixes {-context.xml, Context.groovy}.

Could not detect default configuration classes for test class [xx.xx.Test]: Test does not declare any static, non-private, non-final, nested classes annotated with @Configuration.

测试类添加

package xx.xx.test;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTests { @Before
public void init(){
System.out.println("******测试开始");
} @After
public void end(){
System.out.println("******测试结束");
} @BeforeClass
public static void initClass(){
System.out.println("******测试开始初始化");
} @AfterClass
public static void endClass(){
System.out.println("******测试结束初始化");
}
}
package xx.xx.test;

public class Test extends ApplicationTests{

    @Autowired
private BtsTradeGoodsService btsTradeGoodsService; @org.junit.Test
public void add(){
btsTradeGoodsService.add();
} }

然后在有@Test注解方法的类中使用JUint启动。 在@Test注解的方法中,和平时开发项目调用接口是一样的。

注解

类注解

@RunWith:

  1.表示运行方式,@RunWith(JUnit4TestRunner)、@RunWith(SpringRunner.class)、@RunWith(PowerMockRunner.class) 三种运行方式,分别在不同的场景中使用。

  2.当一个类用@RunWith注释或继承一个用@RunWith注释的类时,JUnit将调用它所引用的类来运行该类中的测试而不是开发者去在junit内部去构建它。我们在开发过程中使用这个特性。

@SpringBootTest:

  1.注解制定了一个测试类运行了Spring Boot环境。提供了以下一些特性:

    1.1.当没有特定的ContextConfiguration#loader()(@ContextConfiguration(loader=...))被定义那么就是SpringBootContextLoader作为默认的ContextLoader。

    1.2.自动搜索到SpringBootConfiguration注解的文件。

    1.3.允许自动注入Environment类读取配置文件。

    1.4.提供一个webEnvironment环境,可以完整的允许一个web环境使用随机的端口或者自定义的端口。

    1.5.注册了TestRestTemplate类可以去做接口调用。

  2.添加这个就能取到spring中的容器的实例,如果配置了@Autowired那么就自动将对象注入。

@WebAppConfiguration:

  由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。

@WebIntegrationTest("server.port:0"):

  使用0表示端口号随机,也可以具体指定如8888这样的固定端口。不可和@WebAppConfiguration同时使用。

方法注解

@BeforeClass:方法只能是static void。

@AfterClass:方法只能是static void。

@Before:@Test运行之前调用的方法,可以做初始化操作

@After:执行完测试用例需要执行的清理工作

@Test:测试用例的单元

@Mock:

  有点类似Autowired注解,而@Mock注解是自动实现模拟对象,而并非Bean实例创建。

  正式环境只是一个接口,并没有实现,也并没有纳入spring容器进行管理。使用BDDMockito对行为进行预测。

@Ignore("not ready yet"):该方法不执行

执行顺序是:@BeforeClass→@Before→@Test→@After→@AfterClass

当启动测试类,测试类中有多个@Test,@BeforeClass和@AfterClass只会执行一次,每一个@Test都会执行一次@Before和@After。

对Controller进行测试

  1、使用浏览器直接访问:

    http://localhost:8080/index     
    http://localhost:8080/show?id=100

  2、使用测试类:

package xx.xx.ctrl;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; @RestController
public class UserCtrl { @GetMapping("/index")
public String index(){
System.out.println("UserCtrl.index");
return "UserCtrl.index";
} @GetMapping("/show")
public String show(@RequestParam("id")String id){
System.out.println("UserCtrl.show:" + id);
return "UserCtrl.show:" + id;
}
}
package xx.xx.test;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ApplicationTests { @Before
public void init(){
System.out.println("******测试开始");
} @After
public void end(){
System.out.println("******测试结束");
} @BeforeClass
public static void initClass(){
System.out.println("******测试开始初始化");
} @AfterClass
public static void endClass(){
System.out.println("******测试结束初始化");
}
}
package xx.xx.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.web.client.TestRestTemplate; public class Test extends ApplicationTests{ @Autowired
private TestRestTemplate testRestTemplate; @org.junit.Test
public void toIndex(){
String t = testRestTemplate.getForObject("/index", String.class);
System.out.println("toIndex:" + t);
} @org.junit.Test
public void toShow(){
String t = testRestTemplate.getForObject("/show?id=99", String.class);
System.out.println("toShow:" + t);
} @Override
public void init(){
System.out.println("******重写测试开始");
}
}

使用这种方式,在@SpringBootTest注解中一定要添加`webEnvironment = SpringBoot

SpringBoot中使用JUnit4(入门篇)的更多相关文章

  1. springboot(一)入门篇

    作者:纯洁的微笑 出处:www.ityouknow.com 版权所有,欢迎保留原文链接进行转载:) 根据原文以下内容略有调整(由于SpringBoot版本更新引起) 什么是spring boot Sp ...

  2. SpringBoot 第一篇:入门篇

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10819728.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   博主从去年 ...

  3. Springboot快速入门篇,图文并茂

    Springboot快速入门篇,图文并茂 文章已托管到GitHub,大家可以去GitHub查看阅读,欢迎老板们前来Star!搜索关注微信公众号 [码出Offer] 领取各种学习资料! image-20 ...

  4. 【Java】在Eclipse中使用JUnit4进行单元测试(初级篇)

    本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,需要写成千上万个方法或函数,这些函数的功能可能 ...

  5. 在Eclipse中使用JUnit4进行单元測试(0基础篇)

    本文绝大部分内容引自这篇文章: http://www.devx.com/Java/Article/31983/0/page/1 我们在编写大型程序的时候,须要写成千上万个方法或函数,这些函数的功能可能 ...

  6. 《Java从入门到放弃》入门篇:hibernate中的多表对应关系

    hibernate中的对应关系其实就是数据库中表的对应关系, 就跟某些电影中的某些场景是一样一样滴. 比如可以是一男一女,还可以是一男多女, 更可以是多男一女,最后最后最后还可以是多男多女!!! 有些 ...

  7. 如何在Visual Studio 2017中使用C# 7+语法 构建NetCore应用框架之实战篇(二):BitAdminCore框架定位及架构 构建NetCore应用框架之实战篇系列 构建NetCore应用框架之实战篇(一):什么是框架,如何设计一个框架 NetCore入门篇:(十二)在IIS中部署Net Core程序

    如何在Visual Studio 2017中使用C# 7+语法   前言 之前不知看过哪位前辈的博文有点印象C# 7控制台开始支持执行异步方法,然后闲来无事,搞着,搞着没搞出来,然后就写了这篇博文,不 ...

  8. SpringBoot的学习一:入门篇

    SpringBoot是什么: SpringBoot是Spring项目中的一个子工程,是一个轻量级框架. SpringBoot框架中有两个个非常重要的策略:开箱即用和约定优于配置 一.构建工程 1.开发 ...

  9. Java中的IO流 - 入门篇

    前言 大家好啊,我是汤圆,今天给大家带来的是<Java中的IO流-入门篇>,希望对大家有帮助,谢谢 由于Java的IO类有很多,这就导致我刚开始学的时候,感觉很乱,每次用到都是上网搜,结果 ...

随机推荐

  1. Vue.js教程 2.体验Vue

    Vue.js教程 2.体验Vue <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  2. Unicode、UTF-8、UTF-16 终于懂了

    计算机起源于美国,上个世纪,他们对英语字符与二进制位之间的关系做了统一规定,并制定了一套字符编码规则,这套编码规则被称为ASCII编码 ASCII 编码一共定义了128个字符的编码规则,用七位二进制表 ...

  3. 3D 穿梭效果?使用 UWP 也能搞定

    昨天 ChokCoco 大佬搞了个 3D 穿梭效果出来,具体可见这里: 3D 穿梭效果?使用 CSS 轻松搞定 这个效果太神奇了,他还问我能不能用 WPF 搞出来,因为我完全没用过 WPF 的 3D, ...

  4. 【JAVA】编程(2)---时间管理

    作业要求: 定义一个名为 MyTime 的类,其中私有属性包括天数,时,分,秒:定义一个可以初始化时,分,秒的构造方法,并对初始化数值加以限定,以防出现bug:定义一个方法,可以把第几天,时,分,秒打 ...

  5. [atAGC007E]Shik and Travel

    二分枚举答案,判定答案是否合法 贪心:每一个叶子只能经过一遍,因此叶子的顺序一定是一个dfs序,即走完一棵子树中的所有叶子才会到子树外 根据这个贪心可以dp,设$f[k][l][r]$表示仅考虑$k$ ...

  6. [第四篇] PostGIS:“我让PG更完美!”

    概要 本篇文章主要分为几何图形处理函数.仿生变换函数.聚类函数.边界分析函数.线性参考函数.轨迹函数.SFCGAL 函数.版本函数这八部分. Geometry Processing ST_Buffer ...

  7. CentOS7部署ceph

    CEPH 简介 不管你是想为云平台提供Ceph 对象存储和/或 Ceph 块设备,还是想部署一个 Ceph 文件系统或者把 Ceph 作为他用,所有 Ceph 存储集群的部署都始于部署一个个 Ceph ...

  8. nodejs中的fs模块中的方法

    nodejs中的fs模块 引入模块 const fs =require("fs") 检测文件是否存在fs.stat(path,callback) fs.stat("./n ...

  9. NOIOL #2 爆零记

    没有假是真的爆零了,原因:万恶的文操.不管怎样写份题解吧. T1: 做题经历:看了下题发现:不是 edu 的原题吗?兴奋地拿出赛中写的程序搞上去. 大约比赛开始 30min 后开始发现 \(k\) 可 ...

  10. Codeforces 1464F - My Beautiful Madness(树的直径)

    Codeforces 题面传送门 & 洛谷题面传送门 树上数据结构大杂烩(?) 首先考虑什么样的点能够在所有路径的 \(d\) 邻居的交集内.显然如果一个点在一条路径的 \(d\) 邻居内则必 ...