有啥不同?来看看Spring Boot 基于 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 的差异
- 忽略测试用例执行
JUnit 4:
@Test @Ignore public void testMethod() { // ... }
JUnit 5:
@Test @Disabled("explanation") public void testMethod() { // ... }
- 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() { } }
- @Before、@BeforeClass、@After、@AfterClass 被替换
@BeforeEach 替换 @Before
@BeforeAll 替换 @BeforeClass
@AfterEach 替换 @After
@AfterAll 替换 @AfterClass
开发环境
JDK 8
示例
1.创建 Spring Boot 工程。
2.添加 spring-boot-starter-web 依赖,最终 pom.xml 如下。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="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"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.6.RELEASE</version> <relativePath/> </parent> <groupId>tutorial.spring.boot</groupId> <artifactId>spring-boot-junit5</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-boot-junit5</name> <description>Demo project for Spring Boot Unit Test with JUnit 5</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <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> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.工程创建好之后自动生成了一个测试类。
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 应用程序上下文。互联网架构师公众号内回复“2T”, 送你全套Java架构视频
4.补充待测试应用逻辑代码
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; } }
5.编写发送 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 层。
6.分层测试。
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 层实例在特定情况下方法调用时的返回结果。
【云栖号在线课堂】每天都有产品技术专家分享!
课程地址:https://yqh.aliyun.com/zhibo立即加入社群,与专家面对面,及时了解课程最新动态!
【云栖号在线课堂 社群】https://c.tb.cn/F3.Z8gvnK

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
吃惊!难道Java也受美国出口管制?
云栖号资讯:【点击查看更多行业资讯】在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来! 今天,去翻看了一下Oracle Jdk的许可协议,竟然是受美国出口管制。 原文是这么说的:EXPORT REGULATIONS. You agree that U.S. export control laws and other applicable export and import laws govern your use of the Software, including technical data; additional information can be found on Oracle's Global Trade Compliance web site (http://www.oracle.com/us/products/export). You agree that neither the Software nor any direct product thereof will be exported, directly, or indirectly, in viol...
- 下一篇
可笑,架构师也能写出这样的Bug
云栖号资讯:【点击查看更多行业资讯】在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来! 小伙话不多,但一旦说话斩钉截铁,带着无法撼动的自信。原因就是,有他着数亿高并发经验,每一秒钟的请求,都是其他企业运行一年也无法企及的。这就让人非常羡慕,毕竟他靠这个比我赚的钱要多。 俗话说,要想在公司不出事故,那就不要写代码。干活多了容易出事,一身轻松无人问津,这就是现实。 但有时候还是要看成果的。新来的研发领导不懂技术,但他懂技术指标,所以就统计大家提交 Git 的数量,如果 Git 活动是一片绿色如 A 股,那就算过关了。 架构师思来想去,决定领一个并发量最高的需求:统计接口的平均响应时间和启动以来的请求数。 为什么说它的并发量高呢?这是因为,它是统计所有接口的,自然比每一个接口的请求量都要大。AOP 代码一包,每个接口都得从他这里走一圈。 该我们的架构师上场了,代码如下图: 架构师说,我的代码不需要做注释。所谓的注释,都是给垃圾代码用的。我深以为是,他明显是受到了 Netflix 公司的影响。 程序考虑到了高并发场景,使用了线程安全的 ConcurrentHashMap,然后每次通...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS7设置SWAP分区,小内存服务器的救世主
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7编译安装Gcc9.2.0,解决mysql等软件编译问题
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS8安装Docker,最新的服务器搭配容器使用
- 设置Eclipse缩进为4个空格,增强代码规范