欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > SpringBoot【实用篇】- 测试

SpringBoot【实用篇】- 测试

2025/4/18 20:29:14 来源:https://blog.csdn.net/m0_74825541/article/details/145272720  浏览:    关键词:SpringBoot【实用篇】- 测试
文章目录
    • 目标:
      • 1.加载测试专用属性
      • 3.Web环境模拟测试
      • 2.加载测试专用配置
      • 4.数据层测试回滚
      • 5.测试用例数据设定

目标:

  • 加载测试专用属性
  • 加载测试专用配置
  • Web环境模拟测试
  • 数据层测试回滚
  • 测试用例数据设定
    在这里插入图片描述
1.加载测试专用属性

我们在前面讲配置高级的时候是这样写的:

test:prop: testValue

测试类:

@SpringBootTest
public class PropertiesAndArgsTest {@Value("${test.prop}")private String msg;@Testvoid testProperties(){System.out.println(msg);}
}

在这里插入图片描述
那如果我把yml文件中的配置注释掉,我们还可以通过@SprinBootTest 来添加临时属性

@SpringBootTest("test.prop = testValue")
public class PropertiesAndArgsTest {@Value("${test.prop}")private String msg;@Testvoid testProperties(){System.out.println(msg);}
}

在这里插入图片描述
如果两个都有谁生效呢? 答案是在这个测试类properties属性添加的临时属性配置中会覆盖yml的配置。
用args配也是可以的,使用args属性可以为当前测试用例添加临时的命令行参数

//@SpringBootTest("test.prop = testValue")
@SpringBootTest(args = {"--test.prop=testValue2"})
public class PropertiesAndArgsTest {@Value("${test.prop}")private String msg;@Testvoid testProperties(){System.out.println(msg);}
}

在这里插入图片描述
那如果三个都有呢?
在这里插入图片描述
答案: 命令行级别参数(源码级别) > properties(idea)
小结:
加载测试临时属性应用小于小范围测试环境.

3.Web环境模拟测试

如果我们要想加入一个外部的bean来辅助我们测试:

@Configuration
public class MsgConfig {@Beanpublic String msg(){return "bean msg";}}

测试类中:

@SpringBootTest
@Import(MsgConfig.class)
public class ConfigurationTest {@Autowiredprivate String msg;@Testvoid testConfiguration() {System.out.println(msg);}}

在这里插入图片描述
在这里插入图片描述

小结: 加载测试范围配置应用与小范围测试环境

能不能在测试样例中测试表现层呢?

2.加载测试专用配置
@SpringBootTest
public class WebTest {@Testvoid test(){}
}

ctrl+左键 =》 查看SpringBootTest的源码,ctrl + f12查看方法
在这里插入图片描述

@SpringBootTest(webEnvironment =SpringBootTest.WebEnvironment.RANDOM_PORT)

在这里插入图片描述

创建controller 然后模拟调用

@RestController
@RequestMapping("/books")
public class BookController {@GetMappingpublic String getById(){System.out.println("getById is running...");return "SpringBoot";}
}@Testvoid testWeb(@Autowired MockMvc mvc) throws Exception {//http:localhost:8080/books//创建虚拟请求 当前访问路径为/booksMockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");mvc.perform(builder);}

ctrl + 左键 进去RequestBuilder查看源码,然后ctrl+h查看实现类
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

   @Testvoid testStatus(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");ResultActions action = mvc.perform(builder);//设定预期值与真实值进行比较,成功测试通过,失败测试失败StatusResultMatchers status = MockMvcResultMatchers.status();//预计本次调用时成功的:状态200ResultMatcher ok = status.isOk();//添加预计值到本次调用过程中进行匹配action.andExpect(ok);}

如果你修改为book1
在这里插入图片描述
在这里插入图片描述
执行结果的匹配

 @Testvoid testBody(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");ResultActions action = mvc.perform(builder);//设定预期值与真实值进行比较,成功测试通过,失败测试失败ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher result = content.string("springboot");action.andExpect(result);}

在这里插入图片描述

但是我们以后是对json做匹配

import lombok.Data;@Data
public class Book {private int id;private String name;private String type;private String description;
}@GetMappingpublic Book getById(){System.out.println("getById is running...");Book book = new Book();book.setId(1);book.setName("SpringBoot");book.setType("Spring Framework");book.setDescription("This is a book about Spring Boot");return book;}

在这里插入图片描述

 @Testvoid testJson(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");ResultActions action = mvc.perform(builder);//设定预期值与真实值进行比较,成功测试通过,失败测试失败ContentResultMatchers content = MockMvcResultMatchers.content();ResultMatcher result = content.json("{
" +"    "id": 1,
" +"    "name": "SpringBoot",
" +"    "type": "Spring Framework",
" +"    "description": "This is a book about Spring Boot"
" +"}");action.andExpect(result);}

我们也可以测试header-type 虚拟请求头匹配

  @Testvoid testContentType(@Autowired MockMvc mvc) throws Exception {MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");ResultActions action = mvc.perform(builder);//设定预期值与真实值进行比较,成功测试通过,失败测试失败HeaderResultMatchers header = MockMvcResultMatchers.header();ResultMatcher contentType = header.string("Content-Type", "application/json;charset=UTF-8");action.andExpect(contentType);}

在这里插入图片描述

4.数据层测试回滚

有一种情况是:当我们测试业务层或者Dao层会留下结果数据,真实的企业开发会生成两个sql文件一个数据库的表创建的sql,一个数据库初始化的sql,但是当我们在开发的时候仍然需要测试仍然会留下数据,但是我们是想着我们测试只是想看看写的代码有没有问题,不需要留下数据,下面说的方法只服务于开发,上线后的另说。

我们可以用事务来进行回滚,

在这里插入图片描述
如何生成随机值来进行测试呢?

5.测试用例数据设定

在这里插入图片描述
在这里插入图片描述
封装实体类:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
小结: 使用随机数据替换固定数据。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词