SpringBoot重点详解--使用Junit进行单元测试
目录
本文将对在Springboot中如何使用Junit进行单元测试进行简单示例和介绍,项目的完整目录层次如下图所示。
添加依赖与配置
为了保证测试的完整性,本工程POM文件中除引入Junit单元测试依赖外,还额外引入了用来测试JDBC和Controller的JPA和WEB依赖。
-
<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>1.5.6.RELEASE</version>
-
</parent>
-
-
<dependencies>
-
<!-- 添加MySQL依赖 -->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
</dependency>
-
<!-- 添加JDBC依赖 -->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-data-jpa</artifactId>
-
</dependency>
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
<!-- 引入单元测试依赖 -->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
</dependencies>
同时,在src/main/resources目录下添加核心配置文件application.properties,内容如下。
-
#########################################################
-
### Spring DataSource -- DataSource configuration ###
-
#########################################################
-
spring.datasource.url=jdbc:mysql://localhost:3306/dev1?useUnicode=true&characterEncoding=utf8
-
spring.datasource.driverClassName=com.mysql.jdbc.Driver
-
spring.datasource.username=root
-
spring.datasource.password=123456
-
-
#########################################################
-
### Java Persistence Api -- Spring jpa configuration ###
-
#########################################################
-
# Specify the DBMS
-
spring.jpa.database = MYSQL
-
# Show or not log for each sql query
-
spring.jpa.show-sql = true
-
# Hibernate ddl auto (create, create-drop, update)
-
spring.jpa.hibernate.ddl-auto = update
-
# Naming strategy
-
#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]
-
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
-
# stripped before adding them to the entity manager)
-
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
ApplicationContext测试
在Springboot中使用Junit进行单元测试的方法很简单,只需要在编写的单元测试类上添加两个注解:@RunWith(SpringRunner.class)和@SpringBootTest。
-
@RunWith(SpringRunner.class) // 等价于使用 @RunWith(SpringJUnit4ClassRunner.class)
-
@SpringBootTest(classes = { MyApplication.class, TestConfig.class })
-
public class ApplicationContextTest {
-
-
@Autowired
-
private ApplicationContext context;
-
-
@Autowired
-
private UserDao userDao;
-
-
@Test
-
public void testUserDao() {
-
userDao.addUser(18, "pengjunlee");
-
}
-
-
@Test
-
public void testConfiguration() {
-
Runnable bean = context.getBean(Runnable.class);
-
Assert.assertNotNull(bean);
-
bean.run();
-
}
-
-
}
UserDao定义如下。
-
@Repository
-
public class UserDao {
-
-
@Autowired
-
private JdbcTemplate jdbcTemplate;
-
-
@Transactional
-
public void addUser(Integer userAge, String userName) {
-
String sql = "insert into tbl_user (age,name) values ('" + userAge + "','" + userName + "');";
-
jdbcTemplate.execute(sql);
-
}
-
}
TestConfig定义如下。
-
/**
-
* @TestConfiguration注解的配置内的Bean仅在测试时装配
-
*/
-
@TestConfiguration
-
public class TestConfig {
-
-
@Bean
-
public Runnable createRunnable(){
-
return ()->{
-
System.out.println("This is a test Runnable bean...");
-
};
-
}
-
}
提示:@SpringBootTest注解的classes可以指定用来加载Spring容器所使用的配置类,@TestConfiguration注解修饰的配置类内的Bean仅在测试的时候才会装配。
Environment测试
我们可以通过@SpringBootTest注解的properties属性向Environment中设置新的属性,也可以通过使用EnvironmentTestUtils工具类来向ConfigurableEnvironment中添加新的属性。
-
@RunWith(SpringRunner.class)
-
@SpringBootTest(properties = { "app.token=pengjunlee" })
-
public class EnvironmentTest {
-
-
@Autowired
-
private Environment env;
-
-
@Autowired
-
private ConfigurableEnvironment cenv;
-
-
@Before
-
public void init() {
-
EnvironmentTestUtils.addEnvironment(cenv, "app.secret=55a4b77eda");
-
}
-
-
@Test
-
public void testEnvironment() {
-
System.out.println(env.getProperty("spring.datasource.url"));
-
Assert.assertEquals("pengjunlee", env.getProperty("app.token"));
-
Assert.assertEquals("55a4b77eda", cenv.getProperty("app.secret"));
-
}
-
-
@Test
-
@Ignore // 忽略测试方法
-
public void testIgnore() {
-
-
System.out.println("你看不见我...");
-
}
-
-
}
扩展:Junit测试用例执行顺序:@BeforeClass ==> @Before ==> @Test ==> @After ==> @AfterClass 。
注意:在使用Junit对Spring容器进行单元测试时,若在src/test/resources 目录下存在核心配置文件,Spring容器将会只加载src/test/resources 目录下的核心配置文件,而不再加载src/main/resources 目录下的核心配置文件。
MockBean测试
我们可以通过@MockBean注解来对那些未添加实现的接口进行模拟测试,预先设定好调用方法期待的返回值,然后再进行测试。
例如,有如下的IUserService接口,定义了一个getUserAge()方法用来根据用户的ID来查询用户的年龄。
-
public interface IUserService {
-
-
Integer getUserAge(Long userId);
-
-
}
使用@MockBean来对IUserService接口进行模拟测试,测试代码如下。
-
@RunWith(SpringRunner.class)
-
public class MockBeanTest {
-
-
@MockBean
-
private IUserService userService;
-
-
@SuppressWarnings("unchecked")
-
@Test(expected = NullPointerException.class)
-
public void testMockBean() {
-
-
BDDMockito.given(userService.getUserAge(2L)).willReturn(Integer.valueOf(18));
-
BDDMockito.given(userService.getUserAge(0L)).willReturn(Integer.valueOf(0));
-
BDDMockito.given(userService.getUserAge(null)).willThrow(NullPointerException.class);
-
-
Assert.assertEquals(Integer.valueOf(18), userService.getUserAge(2L));
-
Assert.assertEquals(Integer.valueOf(0), userService.getUserAge(0L));
-
Assert.assertEquals(Integer.valueOf(0), userService.getUserAge(null));
-
-
}
-
}
Controller测试
在Springboot中可以通过TestRestTemplate和MockMvc来对Controller进行测试,有以下两种情况。
情况一
Controller中未装配任何其他Spring容器中的Bean,例如下面这个控制器。
-
@RestController
-
@RequestMapping("/user")
-
public class UserController01 {
-
-
@GetMapping("/home")
-
public String homeUser(@RequestParam(name = "name", required = true) String userName) {
-
if (null == userName || userName.trim() == "") {
-
return "you are nobody...";
-
}
-
return "This is " + userName + "'s home...";
-
}
-
}
此时无需启动Spring容器,可直接使用MockMvc来对Controller进行模拟测试,测试代码如下。
-
@RunWith(SpringRunner.class)
-
@WebMvcTest(controllers = { UserController01.class })
-
public class ControllerTest01 {
-
-
@Autowired
-
private MockMvc mvc;
-
-
@Test
-
public void testAddUser() throws Exception {
-
mvc.perform(MockMvcRequestBuilders.get("/user/home").param("name", ""))
-
.andExpect(MockMvcResultMatchers.status().isOk())
-
.andExpect(MockMvcResultMatchers.content().string("you are nobody..."));
-
mvc.perform(MockMvcRequestBuilders.get("/user/home").param("name", "pengjunlee"))
-
.andExpect(MockMvcResultMatchers.status().isOk())
-
.andExpect(MockMvcResultMatchers.content().string("This is pengjunlee's home..."));
-
}
-
-
}
情况二
Controller中需装配其他Spring容器中的Bean,例如下面这个控制器。
-
@RestController
-
@RequestMapping("/user")
-
public class UserController02 {
-
-
@Autowired
-
private UserDao userDao;
-
-
@GetMapping("/add")
-
public String addUser(@RequestParam(name = "age", required = false, defaultValue = "0") Integer userAge,
-
@RequestParam(name = "name", required = true) String userName) {
-
if (userAge <= 0 || null == userName || userName.trim() == "") {
-
return "0";
-
}
-
userDao.addUser(userAge, userName);
-
return "1";
-
}
-
-
}
此时除了要启动Spring容器,还需要启动内嵌的WEB环境,有以下两种方法。
方法一
利用TestRestTemplate进行测试。
-
@RunWith(SpringRunner.class)
-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
-
public class ControllerTest02 {
-
-
@Autowired
-
private TestRestTemplate template;
-
-
@Test
-
public void testAddUser() {
-
String result1 = template.getForObject("/user/add?name=pengjunlee", String.class);
-
Assert.assertEquals("0", result1);
-
String result2 = template.getForObject("/user/add?age=20&name=Tracy", String.class);
-
Assert.assertEquals("1", result2);
-
}
-
-
}
方法二
利用MockMvc进行测试。
-
@RunWith(SpringRunner.class)
-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
-
@AutoConfigureMockMvc
-
public class ControllerTest03 {
-
-
@Autowired
-
private MockMvc mvc;
-
-
@Test
-
public void testAddUser() throws Exception {
-
mvc.perform(MockMvcRequestBuilders.get("/user/add").param("name", ""))
-
.andExpect(MockMvcResultMatchers.status().isOk())
-
.andExpect(MockMvcResultMatchers.content().string("0"));
-
mvc.perform(MockMvcRequestBuilders.get("/user/add").param("age", "22").param("name", "pengjunlee"))
-
.andExpect(MockMvcResultMatchers.status().isOk())
-
.andExpect(MockMvcResultMatchers.content().string("1"));
-
}
-
-
}
本文项目源码已上传至CSDN,资源地址:https://download.csdn.net/download/pengjunlee/10394302
原文地址:https://blog.csdn.net/pengjunlee/article/details/80206615
SpringBoot重点详解--使用Junit进行单元测试的更多相关文章
- springboot配置详解
springboot配置详解 Author:SimpleWu properteis文件属性参考大全 springboot默认加载配置 SpringBoot使用两种全局的配置文件,全局配置文件可以对一些 ...
- springboot项目--传入参数校验-----SpringBoot开发详解(五)--Controller接收参数以及参数校验----https://blog.csdn.net/qq_31001665/article/details/71075743
https://blog.csdn.net/qq_31001665/article/details/71075743 springboot项目--传入参数校验-----SpringBoot开发详解(五 ...
- SpringBoot @ConfigurationProperties详解
文章目录 简介 添加依赖关系 一个简单的例子 属性嵌套 @ConfigurationProperties和@Bean 属性验证 属性转换 自定义Converter SpringBoot @Config ...
- Spring Boot2 系列教程 (二) | 第一个 SpringBoot 工程详解
微信公众号:一个优秀的废人 如有问题或建议,请后台留言,我会尽力解决你的问题. 前言 哎呦喂,按照以往的惯例今天周六我的安排应该是待在家学学猫叫啥的.但是今年这种日子就可能一去不复返了,没法办法啊.前 ...
- SpringBoot——配置文件详解【五】
前言 SpringBoot的配置文件 配置文件 SpringBoot使用一个全局的配置文件,配置文件名是固定的. application.properties application.yml 配置文件 ...
- Go语言学习之8 goroutine详解、定时器与单元测试
主要内容: 1.Goroutine2. Chanel3. 单元测试 1. Goroutine Go 协程(Goroutine)(轻量级的线程,开线程没有数量限制). (1)进程和线程 A. 进程是 ...
- matlab考试重点详解
此帖是根据期末考试复习重点补充完成, 由于使用word编辑引用图片和链接略有不便, 所以开此贴供复习及学习使用.侵删 复习要点 第一章 Matlab的基本概念,名称的来源,基本功能,帮助的使用方法 1 ...
- Springboot 启动详解
1.前言 最近一直在看Springboot和springcloud代码,看了将近20多天,对这两个系统的认知总算是入了门.后续应该会有一个系列的文章,本文就先从Springboot的启动入手. 2.容 ...
- 2.SpringBoot HelloWorld详解
1.POM文件 父项目 <parent> <groupId>org.springframework.boot</groupId> <artifactId> ...
随机推荐
- 关于cocos2dx for lua资源加载优化方案
之前我写游戏加载都是从一个json文件写入要加载的文件名来实现加载,但是如果资源 比较多的情况下,会导致非常难管理,需要逐个写入.所以换了另外一种方式来加载文件. 首先,我是通过场景之前的切换时候,加 ...
- C# 获取Google Chrome的书签
其实这个很简单,就是读取一个在用户目录里面的一个Bookmarks文件就好了. 先建立几个实体类 public class GoogleChrome_bookMark_meta_info { publ ...
- CPL学习笔记(一)
整型 计算机的内存的基本单位是位(bit),可以将其看作电子开关,可以开,表示1:也可以关表示0. 字节(byte)通常指八位的内存单元. 8bit=1byte=1B; 1KB=1024B; 1M=1 ...
- matplot绘图(五)
b3D图形绘制 # 导包:from mpl_toolkits.mplot3d.axes3d import Axes3Dimport matplotlib.pyplot as plt%matplotli ...
- 02 Django框架基础(APP的创建访问)
一.创建项目 1.命令:django-admin startproject sitename 2.IDLE环境:本质上都是执行上述命令 常用命令: python manage.py runserver ...
- Ubuntux下简单设置vim
我自己在vim下的设置,基本写简单脚本用的,在~/.vimrc作出如下设置 syntax on "高亮 set nu "行号显示 set smartindent "基于a ...
- 局域网映射到公网-natapp实现
在开发时可能会有这样的需求: 需要将自己开发的机器上的应用提供到公网上进行访问,但是并不想通过注册域名.搭建服务器等等一系列繁琐的操作来实现. 例如:微信公众号的开发调试就需要用到域名访问本机项目. ...
- ubuntu12.04 ppa安装pidgin
sudo apt-get update sudo apt-get dist-upgrade sudo add-apt-repository ppa:pidgin-developers/ppa按下回车 ...
- 函数名&函数名取地址
有时看到如下的代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 /*****************************/ #includ ...
- TTL与COMS的区别
1.电平的上限和下限定义不一样,CMOS具有更大的抗噪区域. 同是5伏供电的话,ttl一般是1.7V和3.5V的样子,CMOS一般是 2.2V,2.9V的样子,不准确,仅供参考. 2.电流驱动能力不 ...