SpringBoot开发中测试工作怎么弄?数据层及Web层环境如何给定测试范围进行测试工作

40 篇文章 0 订阅

写在前面
继续记录自己的SpringBoot学习之旅,这次是SpringBoot应用相关知识学习记录。若看不懂则建议先看前几篇博客,详细代码可在我的Gitee仓库SpringBoot克隆下载学习使用!

3.3 测试

3.3.1 准备

  • 新建空项目,pom文件修改,如图![[Pasted image 20220802170737.png]]
  • controller文件如图![[Pasted image 20220802113156.png]]
  • 测试主要在test目录下进行操作,其它也有主文件目录,目录如图![[Pasted image 20220802161700.png]]
  • yml配置如图![[Pasted image 20220802133122.png]]
  • Mapper、Service包只有保存方法save

3.3.2 加载测试专用属性

  • 在启动测试环境时可以通过properties参数设置环境专用属性,如下:
//properteis属性可以为当前测试用例添加临时的测试属性配置  
@SpringBootTest(properties = {"test.properties = value1"})
public class TestProperties {
    @Value("${test.properties}")  
    private String properties;  
    @Test  
    public void TestProperties() {  
        System.out.println(properties);  
    }  
}
  • 在启动测试环境时可以通过args参数设置环境专用属性,如下:
//arges可以在当前测试用例添加临时的命令行参数  
@SpringBootTest(args = {"--test.properties=value2"})
public class TestProperties {
    @Value("${test.properties}")  
    private String properties;  
    @Test  
    public void TestProperties() {  
        System.out.println(properties);  
    }  
}
  • 加载测试临时属性应用于小范围测试

3.3.3 加载测试专用配置

使用@Import注解加载当前测试类专用配置

@SpringBootTest  
@Import(MSGConfig.class)  
public class ConfigurationTest {  
  
    @Autowired  
    public String msg;  
    @Test  
    public void testConfiguration(){  
        System.out.println(msg);  
    }  
}
  • 加载测试范围应用于小范围测环境

3.3.4 Web环境模拟测试

  • 模拟端口,如下:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)  
public class WebTest {  
    @Test  
    public void WebTest() {  
    }

注:其中WebEnvironment参数:
- MOCK:不启动
- DEFAULT_PORT:默认端口号8080
- RANDOM_PORT:随机端口号
- NONE:不启动WEB环境

  • 虚拟请求,如下:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT )  
//开启虚拟模拟MVC调用  
@AutoConfigureMockMvc  
public class WebTest {  
    @Test  
    public void WebTest(@Autowired MockMvc mockMvc) throws Exception {  
        //创建虚拟请求,访问/users  
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users/findUserByID");  
        //执行请求  
        mockMvc.perform(builder);  
    }  
}
  • 虚拟请求状态匹配,如下:
    @Test  
    public void WebTestStatus(@Autowired MockMvc mockMvc) throws Exception {  
        //创建虚拟请求,访问/users  
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users/findUserByI");  
        //执行请求  
        ResultActions actions = mockMvc.perform(builder);  
        //设定预期值 ,与真实值进行比较  
        //定义本次调用预期值  
        StatusResultMatchers status = MockMvcResultMatchers.status();  
//        预期值200与与真实值进行比较  
        actions.andExpect(status.isOk());  
    }

成功不会有什么显示,失败会输出提示,如图
![[Pasted image 20220802111314.png]]

  • 虚拟请求体匹配,如下:
@Test  
    public void WebTestBody(@Autowired MockMvc mockMvc) throws Exception {  
        //创建虚拟请求,访问/users  
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users/findUserByID");  
        //执行请求  
        ResultActions actions = mockMvc.perform(builder);  
        //设定预期值 ,与真实值进行比较  
        //定义本次调用预期值  
        ContentResultMatchers content = MockMvcResultMatchers.content();  
//        预期值与与真实值进行比较  
        actions.andExpect(content.string("findUserBy"));  
    }

失败则会提示如图![[Pasted image 20220802112144.png]]

  • 虚拟请求体JSON匹配,如下:
 @Test  
    public void WebTestJSON(@Autowired MockMvc mockMvc) throws Exception {  
        //创建虚拟请求,访问/users  
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users/findUser");  
        //执行请求  
        ResultActions actions = mockMvc.perform(builder);  
        //设定预期值 ,与真实值进行比较  
        //定义本次调用预期值  
        ContentResultMatchers content = MockMvcResultMatchers.content();  
        ResultMatcher json = content.json("{\"id\":1,\"username\":\"李四\",\"password\":\"password\"}");  
//        预期值与与真实值进行比较  
        actions.andExpect(json);  
    }

失败如图提示![[Pasted image 20220802113817.png]]

  • 虚拟请求头匹配,如下:
    @Test  
    public void WebTestContentType(@Autowired MockMvc mockMvc) throws Exception {  
        //创建虚拟请求,访问/users  
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/users/findUserByID");  
        //执行请求  
        ResultActions actions = mockMvc.perform(builder);  
        //设定预期值 ,与真实值进行比较  
        //定义本次调用预期值  
        HeaderResultMatchers header = MockMvcResultMatchers.header();  
        ResultMatcher json = header.string("Content-Type","application/json");  
//        预期值与与真实值进行比较  
        actions.andExpect(json);  
    }

同理,失败会提示!

3.3.5 数据层测试回滚

3.3.5.1 提要

使用上节知识点所用项目

3.3.5.2 操作
  • 为测试用例添加事务,SpringBoot会对测试用例对应的事务提交操作进行回滚,如下:
@SpringBootTest  
@Transactional  
public class ServiceTest {  
    @Autowired  
    private IUserService iUserService;  
    @Test  
    public void saveUser(){  
        User user = new User();  
        user.setId(18);  
        user.setUsername("李四");  
        user.setPassword("password");  
        iUserService.save(user);  
    }
  • 若要在测试用例中提交事务,可以通过@RollBack注解设置,如图![[Pasted image 20220802171439.png]]

3.3.6 测试用例数据设定

  • 测试用例数据通常采用随机值进行测试,使用SpringBoot提供的随机数为其赋值,在yml进行配置如下:
#    测试随机用例  
testcase:  
  username:  
    id: ${random.int}  
    id2: ${random.int(10,20)}  
    name: 李四${random.value}  
    password: ${random.value}  
    uuid: ${random.uuid}

注:
- ${random.int} 表示随机整数
- ${random.int(10,20)} 表示10-20以内的数
- ${random.value}表示32位MD5字符串
- 其中()可以是任意非敏感字符,如{} !!

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 12
    评论
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值