《SpringBoot篇》14.@AutoConfigureMockMvc测试类实现Web层测试

简介: 《SpringBoot篇》14.@AutoConfigureMockMvc测试类实现Web层测试

1.测试类实现Web层测试


说明:本文是在springboot项目中实现的哦。


(1)在测试类上添加@AutoConfigureMockMvc

@SpringBootTest()
@AutoConfigureMockMvc
class DemoApplicationTests {
    @Test
    void WebFindTest() {
    }
}

(2)在SpringBootTest注解中添加参数webEnvironment

SpringBootTest.WebEnvironment.RANDOM_PORT
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//开启虚拟MVC调用
@AutoConfigureMockMvc
public class WebTest {
    @Test
    void testWeb(@Autowired MockMvc mvc) {
    }
}


注:这里WebEnvironment几个参数的含义:


MOCK:根据当前设置确认是否启动web环境,例如使用了Servlet的API就启动web环境,属于适配性的配置

DEFINED_PORT:使用自定义的端口作为web服务器端口

RANDOM_PORT:使用随机端口作为web服务器端口( 建议使用,防止端口冲突)

NONE:不启动web环境

(3)引入MockMvc对象创建访问请求

注:测试可以发现请求成功,测试发送请求可以成功但是无法看到结果,在下一步就可以看到了。

注:web层Controller代码

package com.example.demo.controller;
import com.example.demo.pojo.Admin;
import com.example.demo.repository.AdminRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class WebController {
    @GetMapping("findWeb")
    @ResponseBody
    public String finall(){
        return "Web测试成功";
    }
}

注:测试类代码


package com.example.demo;
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.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class DemoApplicationTests {
    //引入MockMvc类型对象
    @Autowired
    private MockMvc mvc;
    @Test
    void WebFindTest() throws Exception {
        //http://localhost:8080/findWeb
        //创建虚拟请求
        //前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/findWeb");
        //这里需要抛出异常
        mvc.perform(builder);
    }
}

测试成功效果:

image.png


(4)实现请求结果对比

注: 响应对比有四种类型,响应状态匹配、响应体匹配(非json数据格式)、响应体匹配(json数据格式,开发中的主流使用方式)、响应头信息匹配。


a.响应状态匹配

package com.example.demo;
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.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class DemoApplicationTests {
    //引入MockMvc类型对象
    @Autowired
    private MockMvc mvc;
    @Test
    void WebFindTest() throws Exception {
        //http://localhost:8080/findWeb
        //创建虚拟请求
        //前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/findWeb");
        //这里需要抛出异常
        //获取返回值
    ResultActions perform = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次调用的预期值
        StatusResultMatchers status = MockMvcResultMatchers.status();
        //预计本次调用时成功的:状态200
        ResultMatcher ok = status.isOk();
        //添加预计值到本次调用过程中进行匹配
        perform.andExpect(ok);
    }
}

b.响应体匹配(非json数据格式)

package com.example.demo;
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.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class DemoApplicationTests {
    //引入MockMvc类型对象
    @Autowired
    private MockMvc mvc;
    @Test
    void WebFindTest() throws Exception {
        //http://localhost:8080/findWeb
        //创建虚拟请求
        //前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/findWeb");
        //这里需要抛出异常
        //获取返回值
        ResultActions perform = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次调用的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //预计本次调用时成功的:状态200
        ResultMatcher resultMatcher = content.string("Web测试成功");
        //添加预计值到本次调用过程中进行匹配
        perform.andExpect(resultMatcher);
    }
}

测试成功将会访问成功,给大家看一下失败的样式:(会报错)


image.png

c.响应体匹配(json数据格式,开发中的主流使用方式)

Controller类也要改成返回Json


package com.example.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class WebController {
    @GetMapping("findWeb")
    @ResponseBody
    public String finall(){
        return "{\"name\" : Web测试成功}";
    }
}


注意测试类Json写法:


package com.example.demo;
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.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class DemoApplicationTests {
    //引入MockMvc类型对象
    @Autowired
    private MockMvc mvc;
    @Test
    void WebFindTest() throws Exception {
        //http://localhost:8080/findWeb
        //创建虚拟请求
        //前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/findWeb");
        //这里需要抛出异常
        //获取返回值
        ResultActions perform = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次调用的预期值
        ContentResultMatchers content = MockMvcResultMatchers.content();
        //这里注意Json写法格式
        ResultMatcher result = content.json("{\"name\":Web测试成功}");
        //添加预计值到本次调用过程中进行匹配
        perform.andExpect(result);
    }
}

d.响应头信息匹配

package com.example.demo;
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.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.HeaderResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class DemoApplicationTests {
    //引入MockMvc类型对象
    @Autowired
    private MockMvc mvc;
    @Test
    void WebFindTest() throws Exception {
        //http://localhost:8080/findWeb
        //创建虚拟请求
        //前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/findWeb");
        //这里需要抛出异常
        //获取返回值
        ResultActions perform = mvc.perform(builder);
        //响应头
        HeaderResultMatchers header = MockMvcResultMatchers.header();
        ResultMatcher contentType = header.string("Content-Type", "text/plain;charset=UTF-8");
        perform.andExpect(contentType);
    }
}

(4)校验总结

注: 平时都是三种校验一起,(Json与非Json只能一种)


package com.example.demo;
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.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.ContentResultMatchers;
import org.springframework.test.web.servlet.result.HeaderResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.result.StatusResultMatchers;
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
class DemoApplicationTests {
    //引入MockMvc类型对象
    @Autowired
    private MockMvc mvc;
    @Test
    void WebFindTest() throws Exception {
        //http://localhost:8080/findWeb
        //创建虚拟请求
        //前面的服务器IP地址和端口使用的是当前虚拟的web环境,无需指定,仅指定请求的具体路径即可
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/findWeb");
        //这里需要抛出异常
        //获取返回值
        ResultActions perform = mvc.perform(builder);
        //设定预期值 与真实值进行比较,成功测试通过,失败测试失败
        //定义本次调用的预期值
        //响应状态
        StatusResultMatchers status = MockMvcResultMatchers.status();
        ResultMatcher ok = status.isOk();
        perform.andExpect(ok);
        //响应头
        HeaderResultMatchers header = MockMvcResultMatchers.header();
        ResultMatcher contentType = header.string("Content-Type", "text/plain;charset=UTF-8");
        perform.andExpect(contentType);
        //返回的响应体
        ContentResultMatchers content = MockMvcResultMatchers.content();
        ResultMatcher result = content.json("{\"name\":Web测试成功}");
        perform.andExpect(result);
    }
}

2.补充测试层实现数据回滚


说明:使用@Transactional,@Rollback(true)防止执行测试类产生垃圾数据。


package com.example.demo;
import com.example.demo.pojo.Admin;
import com.example.demo.repository.AdminRepository;
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.annotation.Rollback;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.HeaderResultMatchers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
@SpringBootTest()
@Transactional
@Rollback
class RollbackTests {
    @Autowired
    private AdminRepository adminRepository;
    @Test
    void WebFindTest()  {
        Admin admin  = new Admin();
        admin.setPassword("123");
        adminRepository.save(admin);
    }
}

3.补充测试用例数据随机


说明: 在配置文件中设置数据为随机,配合@ConfigurationProperties实现随机值注入


test:
  book:
    id: ${random.int}
    id2: ${random.int(10)}
    type: ${random.int!5,10!}
    name: ${random.value}
    uuid: ${random.uuid}
    publishTime: ${random.long}
@Component
@Data
@ConfigurationProperties(prefix = "test.book")
public class BookCase {
    private int id;
    private int id2;
    private int type;
    private String name;
    private String uuid;
    private long publishTime;
}


相关文章
|
1月前
|
IDE Java 测试技术
使用JUnit进行单元测试:提高Java Web应用的稳定性和可靠性
【4月更文挑战第3天】本文介绍了JUnit,一个广泛使用的Java单元测试框架,由Kent Beck和Erich Gamma创建。JUnit核心特性包括注解、断言、测试套件、测试监听器和异常测试。在Java Web应用中,单元测试主要针对模型层。使用JUnit测试涉及设置环境、编写测试类、标记测试方法及运行和分析结果。单元测试能提早发现问题、简化调试、保证代码质量、促进重构并作为实时文档。掌握JUnit对提升软件质量和效率至关重要。
|
2月前
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
Mybatis+mysql动态分页查询数据案例——测试类HouseDaoMybatisImplTest)
22 1
|
2月前
|
Java 测试技术 Spring
Spring Boot 基于 JUnit 5 实现单元测试
Spring Boot 基于 JUnit 5 实现单元测试
28 0
|
1月前
|
Java 测试技术
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
SpringBoot整合单元测试&&关于SpringBoot单元测试找不到Mapper和Service报java.lang.NullPointerException的错误
22 0
|
4天前
|
SQL 安全 数据处理
Web 测试神器:HackBar 保姆级教程
Web 测试神器:HackBar 保姆级教程
|
10天前
|
Java 测试技术 Maven
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
Spring Boot单元测试报错java.lang.IllegalStateException: Could not load TestContextBootstrapper [null]
|
14天前
|
安全 Java 测试技术
Spring Boot 自动化单元测试类的编写过程
企业开发不仅要保障业务层与数据层的功能安全有效,也要保障表现层的功能正常。但是我们一般对表现层的测试都是通过postman手工测试的,并没有在打包过程中代码体现表现层功能被测试通过。那么能否在测试用例中对表现层进行功能测试呢?答案是可以的,我们可以使用MockMvc来实现它。
42 0
|
16天前
|
Java 测试技术 开发者
[AIGC] 使用Spring Boot进行单元测试:一份指南
[AIGC] 使用Spring Boot进行单元测试:一份指南
|
16天前
|
Java 测试技术 数据库
【SpringBoot】连接数据源并回显(附加单元测试)
【SpringBoot】连接数据源并回显(附加单元测试)
15 0
|
18天前
|
XML Web App开发 测试技术
python的Web自动化测试
【4月更文挑战第16天】Python在Web自动化测试中广泛应用,借助Selenium(支持多浏览器交互)、BeautifulSoup(解析HTML/XML)、Requests(发送HTTP请求)和Unittest(测试框架)等工具。测试步骤包括环境搭建、编写测试用例、初始化浏览器、访问页面、操作元素、验证结果、关闭浏览器及运行报告。注意浏览器兼容性、动态内容处理和错误处理。这些组合能提升测试效率和质量。
15 6