spring boot 单元测试_有啥不同?来看看Spring Boot 基于 JUnit 5 实现单元测试

作者:又语

https://www.jianshu.com/p/4648fd55830e

目录

  • 简介

  • JUnit 4 和 JUnit 5 的差异

  1. 忽略测试用例执行

  2. RunWith 配置

  3. @Before@BeforeClass@After@AfterClass 被替换

开发环境

示例

简介

本文介绍 Spring Boot 2 基于 JUnit 5 的单元测试实现方案。

Spring Boot 2.2.0 版本开始引入 JUnit 5 作为单元测试默认库,在 Spring Boot 2.2.0 版本之前,spring-boot-starter-test 包含了 JUnit 4 的依赖,Spring Boot 2.2.0 版本之后替换成了 Junit Jupiter。


JUnit 4 和 JUnit 5 的差异

1. 忽略测试用例执行

JUnit 4:

@Test
@Ignore
public void testMethod() {

}

JUnit 5:

@Test
@Disabled("explanation")
public void testMethod() {

}
2. RunWith 配置

JUnit 4:

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void contextLoads() {
}
}

JUnit 5:

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class ApplicationTests {
@Test
public void contextLoads() {
}
}
3. @Before@BeforeClass@After@AfterClass 被替换
  • @BeforeEach 替换 @Before

  • @BeforeAll 替换 @BeforeClass

  • @AfterEach 替换 @After

  • @AfterAll 替换 @AfterClass


开发环境

  • JDK 8


示例

  1. 创建 Spring Boot 工程,参考:IntelliJ IDEA 创建 Spring Boot 工程。

  2. 添加 spring-boot-starter-web 依赖,最终 pom.xml 如下。

<?xml version="1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">4.0.0org.springframework.bootspring-boot-starter-parent2.2.6.RELEASEtutorial.spring.bootspring-boot-junit50.0.1-SNAPSHOTspring-boot-junit5Demo project for Spring Boot Unit Test with JUnit 51.8org.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtestorg.junit.vintagejunit-vintage-engineorg.springframework.bootspring-boot-maven-plugin
  1. 工程创建好之后自动生成了一个测试类。

package tutorial.spring.boot.junit5;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootJunit5ApplicationTests {

@Test
void contextLoads() {
}

}

这个测试类的作用是检查应用程序上下文是否可正常启动。@SpringBootTest 注解告诉 Spring Boot 查找带 @SpringBootApplication 注解的主配置类,并使用该类启动 Spring 应用程序上下文。

  1. 补充待测试应用逻辑代码

4.1. 定义 Service 层接口

package tutorial.spring.boot.junit5.service;

public interface HelloService {

String hello(String name);
}

4.2. 定义 Controller 层

package tutorial.spring.boot.junit5.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import tutorial.spring.boot.junit5.service.HelloService;

@RestController
public class HelloController {

private final HelloService helloService;

public HelloController(HelloService helloService) {
this.helloService = helloService;
}

@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
return helloService.hello(name);
}
}

4.3. 定义 Service 层实现

package tutorial.spring.boot.junit5.service.impl;

import org.springframework.stereotype.Service;
import tutorial.spring.boot.junit5.service.HelloService;

@Service
public class HelloServiceImpl implements HelloService {

@Override
public String hello(String name) {
return "Hello, " + name;
}
}
  1. 编写发送 HTTP 请求的单元测试。

package tutorial.spring.boot.junit5;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
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.boot.web.server.LocalServerPort;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class HttpRequestTest {

@LocalServerPort
private int port;

@Autowired
private TestRestTemplate restTemplate;

@Test
public void testHello() {
String requestResult = this.restTemplate.getForObject("http://127.0.0.1:" + port + "/hello/spring",
String.class);
Assertions.assertThat(requestResult).contains("Hello, spring");
}
}

说明:

  • webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT 使用本地的一个随机端口启动服务;

  • @LocalServerPort 相当于 @Value("${local.server.port}")

  • 在配置了 webEnvironment 后,Spring Boot 会自动提供一个 TestRestTemplate 实例,可用于发送 HTTP 请求。

  • 除了使用 TestRestTemplate 实例发送 HTTP 请求外,还可以借助 org.springframework.test.web.servlet.MockMvc 完成类似功能,代码如下:

package tutorial.spring.boot.junit5.controller;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@SpringBootTest
@AutoConfigureMockMvc
public class HelloControllerTest {

@Autowired
private HelloController helloController;

@Autowired
private MockMvc mockMvc;

@Test
public void testNotNull() {
Assertions.assertThat(helloController).isNotNull();
}

@Test
public void testHello() throws Exception {
this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("Hello, spring"));
}
}

以上测试方法属于整体测试,即将应用上下文全都启动起来,还有一种分层测试方法,譬如仅测试 Controller 层。

  1. 分层测试。

package tutorial.spring.boot.junit5.controller;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import tutorial.spring.boot.junit5.service.HelloService;

@WebMvcTest
public class HelloControllerTest {

@Autowired
private HelloController helloController;

@Autowired
private MockMvc mockMvc;

@MockBean
private HelloService helloService;

@Test
public void testNotNull() {
Assertions.assertThat(helloController).isNotNull();
}

@Test
public void testHello() throws Exception {
Mockito.when(helloService.hello(Mockito.anyString())).thenReturn("Mock hello");
this.mockMvc.perform(MockMvcRequestBuilders.get("/hello/spring"))
.andDo(MockMvcResultHandlers.print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.content().string("Mock hello"));
}
}

说明:

  • @WebMvcTest 注释告诉 Spring Boot 仅实例化 Controller 层,而不去实例化整体上下文,还可以进一步指定仅实例化 Controller 层的某个实例:@WebMvcTest(HelloController.class)

  • 因为只实例化了 Controller 层,所以依赖的 Service 层实例需要通过 @MockBean 创建,并通过 Mockito 的方法指定 Mock 出来的 Service 层实例在特定情况下方法调用时的返回结果。

更多精彩?在公众号对话框输入以下关键词查看更多优质内容!命令行 | 人工智能 | 软件测试 | Web前端 | Python 大数据Java | 1024 | 电子书 | 速查表 | 机器学习笔记 | Python进阶面试 | 手册 | 成神 | 思想 | 0011

887e7efa035402cd0852ed8541cf11e3.png

扫码关注我们

获取更多学习资料

视频 | 面试 | 技术 | 电子书 

程序员编程:javaje

小编微信:BW22266688

我知道你“在看”

f5f0f2767f3e8e30d145d446ecd82fb5.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值