Spring Boot基于JUint5实现单元测试

news/2024/7/8 13:09:47

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


目录

  • 简介
  • JUnit 4 和 JUnit 5 的差异
    1. 忽略测试用例执行
    2. RunWith 配置
    3. @Before@BeforeClass@After@AfterClass 被替换
  • 开发环境
  • 示例

简介

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"?>
<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>
  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 层实例在特定情况下方法调用时的返回结果。

http://www.niftyadmin.cn/n/3156037.html

相关文章

socke5 使用curl 测试_命令行测试WebSocket

使用命令测试WebSocketLinux环境下&#xff0c;分别使用curl和wscat命令测试websocket连接。前言有时候我们需要从后台验证WebSocket连接是否正常&#xff0c;判断防火墙是否开通&#xff0c;反向代理是否配置正确等。我一般用下面两种方式进行快速简单的测试。一、使用wscat测…

场景下开发-$http服务封装

$http服务封装 .factory(htp,function(){// htp.runServiceWithSession() // htp.runService() // htp.runService($http, $ionicLoading, "member.register", // $scope.user, function(data, status){}// 错误处理 函数function callErrorF…

IDEA设置默认Maven

idea设置默认maven配置, 避免每次设置maven Step 1&#xff1a;Other Setting--->Default Settings--->设置maven--->Apply--->OK

JS控制div跳转到指定的位置的几种解决方案总结

原文&#xff1a;http://www.jb51.net/article/96574.htm 这篇文章主要介绍了JS控制div跳转到指定的位置的几种解决方案总结&#xff0c;小编觉得挺不错的&#xff0c;现在分享给大家&#xff0c;也给大家做个参考。 总结一下自己在写这个需求遇到的问题&#xff0c;相信大家应…

localdate判断为null_Java8之使用Optional进行Null处理

Optional类这是Java 8新增的一个类&#xff0c;用以解决程序中常见的NullPointerException异常问题&#xff0c;本篇文章将详细介绍Optional类&#xff0c;以及如何用它消除代码中的null检查。1.创建optional对象empty() 方法用于创建一个没有值的Optional对象&#xff1a;Opti…

netfilter 结构整理

(Linux network) netfilter struct 整理 结构体关系图 阐述了结构体之间的关联xt_table 在net->netns_ipv4结构体中&#xff0c;包含了如下几个xt_tableiptable_filteriptable_mangleiptable_rawarptable_filternat_filterstruct xt_table {struct list_head list;//xt_tabl…

图片空间-网盘

就是酱紫的一个东西&#xff0c;类似于一个网盘&#xff0c;只不过它仅仅用于存储图片。 完成的初级效果图。这里的ztree我用的是metro的那个&#xff0c;但是并未引用他的js&#xff0c;同时也带来一些css样式问题&#xff1a;1、叶子图标我要显示为文件夹图标&#xff0c; 2、…

绿盟科技网络安全威胁周报2017.01 请关注MatrixSSL堆缓冲区溢出漏洞CVE-2016-6890

绿盟科技发布了本周安全通告&#xff0c;周报编号NSFOCUS-16-39&#xff0c;绿盟科技漏洞库本周新增30条&#xff0c;其中高危9条。本次周报建议大家关注 MatrixSSL 堆缓冲区溢出漏洞 &#xff0c;目前&#xff0c;此漏洞已经公布了一段时间并且官方已经进行了修复&#xff0c;…