当前位置: 首页 > news >正文

SpringBoot测试实践

测试按照粒度可分为3层:

  1. 单元测试:单元测试(Unit Testing)又称为模块测试 ,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作。程序单元是应用的最小可测试部件。在过程化编程中,一个单元就是单个程序、函数、过程等;对于面向对象编程,最小单元就是方法,包括基类(超类)、抽象类、或者派生类(子类)中的方法。
  2. 集成测试:整合测试(Integration Testing),又称组装测试,即对程序模块采用一次性或增值方式组装起来,对系统的接口进行正确性检验的测试工作。整合测试一般在单元测试之后、系统测试之前进行。实践表明,有时模块虽然可以单独工作,但是并不能保证组装起来也可以同时工作。该测试,可以由程序员或是软件品保工程师进行。
  3. 端到端测试:端到端测试(End To End Testing),又称系统测试。

在这里插入图片描述

通常需求开发后需要经过RD单测&自测后进行提测,提测往往需要达到一定的单测/自测代码覆盖率,或者某些基本case通过(冒烟测试),符合提测要求后QA对整体功能进行端到端测试。

完善的测试流程有助于提升代码质量和研发效率,这中间一方面对RD自身的业务素养有要求,另一方面对团队研发流程的规范性有要求。

成熟的研发流程和体系应减少“人性”带来的不稳定性,测试即是应对该不稳定性的有效方法之一。

本文记录了结合SpringBoot进行测试的一些案例,示例代码参见: spring-boot-test-sample

注意区分JUnit4和JUnit5的注解,本文代码基于JUnit4
{: .prompt-warning }

首先我们引入依赖:

<dependencies><!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-junit-jupiter</artifactId><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-module-junit4</artifactId><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-api-mockito2</artifactId><scope>test</scope></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-dependencies</artifactId><version>2.6.13</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-core</artifactId><version>2.28.2</version><scope>test</scope></dependency><dependency><groupId>org.mockito</groupId><artifactId>mockito-junit-jupiter</artifactId><version>2.28.2</version><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-module-junit4</artifactId><version>2.0.2</version><scope>test</scope></dependency><dependency><groupId>org.powermock</groupId><artifactId>powermock-api-mockito2</artifactId><version>2.0.2</version><scope>test</scope></dependency></dependencies></dependencyManagement>

Mockito & PowerMockito 单元测试

当我们仅仅需要验证代码逻辑,不需要Spring的bean注入时,使用Mockito & PowerMockito来快速测试。

Mockito用于mock对象便于对代码逻辑进行测试&验证,但Mockito mock的方法有限,无法mock final、private、static方法,而PowerMockito框架弥补了这一点。两者可以混合使用。

案例:


@RunWith(PowerMockRunner.class)
// mock static method
@PrepareOnlyThisForTest({SampleUtil.class})
@PowerMockIgnore({"javax.net.ssl.*","javax.management.*", "javax.security.*", "javax.crypto.*"})
public class UnitTest {@Mockprivate SampleRepository sampleRepository;@InjectMocksprivate SampleService sampleService;@BeforeClasspublic static void beforeAll(){System.out.print("\n\n\n++++++++++++++\n\n\n");}@AfterClasspublic static void afterAll(){System.out.print("\n\n\n==============\n\n\n");}@Beforepublic void before(){}@Afterpublic void after(){}@Testpublic void getSamples() throws JSONException {PowerMockito.mockStatic(SampleUtil.class);// 注意所有when内部的方法参数必须用org.mockito.ArgumentMatchers的方法包一层,不能直接传PowerMockito.when(SampleUtil.getSomething(eq("1"))) // 反例:.when(SampleUtil.getSomething("1")) .thenReturn(1L);PowerMockito.when(sampleRepository.selectSamples(argThat(id -> id.equals(1L)))).thenReturn(new ArrayList<>());PowerMockito.when(sampleRepository.selectSamples(argThat(new GreaterOrEqual<>(1L)))).thenReturn(new ArrayList<>());// 这里有any(),anyString()等// 如果参数是String,mock方法传入的是null,则mock不生效,传null需指定为any()Mockito.when(sampleRepository.selectSamples(any())).thenReturn(new ArrayList<>());// verify方法调用次数Mockito.verify(sampleRepository, Mockito.times(1)).selectSamples(any());// Mockito.verify(sampleRepository, Mockito.times(1)).selectSamples(argThat(i->i.equals(1)));// capture参数验证ArgumentCaptor<Long> paramCap = ArgumentCaptor.forClass(Long.class);Mockito.verify(sampleRepository, Mockito.times(1)).selectSamples(paramCap.capture());Assert.assertNotNull(paramCap.getValue());List<Sample> samples = sampleService.listSamples("1");// 如果sample.size()返回Long,需要加一个 sample.size().longValue()方法Assert.assertEquals(0,samples.size());// 比较JSONJSONAssert.assertEquals("{\"a\":1}","{\"a\":1}",false);// 解析JSONAssert.assertEquals(JsonPath.parse("{\"a\":1}").read("$.a").getClass(),Integer.class);}@Testpublic void mockPrivate() {try {Method method = PowerMockito.method(Sample.class, "privateMethodName", Long.class);method.invoke(sampleService, 0L);Assert.fail();} catch (Exception e) {Assert.assertEquals("报错信息", e.getCause().getMessage());}}}

@Mock和@MockBean使用格式:Mockito.when(localVar.method()).thenXxx…

@Spy和@SpyBean使用格式:Mockito.doXxx().when(localVar).method()

Spring 测试

当依赖Spring时,可以利用Spring和PowerMockito一起完成mock和test

案例:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@PrepareOnlyThisForTest({SampleUtil.class})
@ContextConfiguration(classes = ControllerSliceTestWithPowerMockito.Context.class)
public class ControllerSliceTestWithPowerMockito {// @Import加入需要扫描的Bean// @Configuration配合其他都行,参考@ContextConfiguration注释@Import(SampleController.class)static class Context {}@MockBeanprivate SampleService sampleService;@SpyBeanprivate SampleConverter sampleConverter;@Testpublic void zkSetup() {PowerMockito.mockStatic(SampleUtil.class);PowerMockito.when(SampleUtil.getSomething(eq("a"))).thenReturn(1L);sampleConverter.test();// assert, verify}}

WebMvc 切片测试

  • @AutoConfigureWebMvc : Use this if you need to configure the web layer for testing but don’t need to use MockMvc
  • @AutoConfigureMockMvc : Use this when you just want to configure MockMvc
  • @WebMvcTest : Includes both the @AutoConfigureWebMvc and the @AutoConfigureMockMvc, among other functionality.

三者区别,参考:What’s the difference between @AutoConfigureWebMvc and @AutoConfigureMockMvc?

案例一:

@WebMvcTest(SampleController.class)
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = TestSampleController.TestContext.class)
public class TestSampleController {private static final Logger log = LoggerFactory.getLogger(TestSampleController.class);// 这里填入需要扫描的Bean,这样就不用扫描整个project文件,加快测试速度@Import({SampleController.class, ControllerExceptionAdvice.class})@Configuration // 这里兼容老版本,高版本不用加static class TestContext {}@Autowiredprivate MockMvc mockMvc;@MockBeanprivate SampleService sampleService;// 这里用SpyBean注解:当SampleController中用到了SampleConverter,但是又不需要mock,得用converter原本的逻辑// 或用@MockBean时,在 Mockito.when(...).thenCallRealMethod()就行。@SpyBeanprivate SampleConverter sampleConverter;@Beforepublic void prepareMock() {// 对SampleController中调用了的SampleService的方法进行mockMockito.doNothing().when(sampleService).sampleMethod(Mockito.any());}@Testpublic void shouldReturnSuccess() throws Exception {SampleRequest req = new SampleRequest();req.setA(1L);String bodyJson = JsonUtils.toJson(req);mockMvc.perform(MockMvcRequestBuilders.post("/test").contentType(MediaType.APPLICATION_JSON).content(bodyJson)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.content().json("{\"success\":true}"));}@Testpublic void shouldReturnErrorMsg() throws Exception {SampleRequest req = new SampleRequest();req.setBString bodyJson = JsonUtils.toJson(req);mockMvc.perform(MockMvcRequestBuilders.post("/test2").contentType(MediaType.APPLICATION_JSON).content(bodyJson)).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.content().contentType(MediaType.APPLICATION_JSON_UTF8)).andExpect(MockMvcResultMatchers.content().json("{\"success\":false,\"errorMsg\":\"错误信息\"}"));}
}

案例二:


@RunWith(PowerMockRunner.class)
@SuppressStaticInitializationFor("com.dianping.cat.Cat")
// mock static method
@PrepareForTest({SampleUtil.class})
// spring bean
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@PowerMockIgnore({"javax.net.ssl.*","javax.management.*", "javax.security.*", "javax.crypto.*"})
// @SpringBootTest从当前包向上找@SpringBootConfiguration,或者指定
@SpringBootTest(classes = SpringTestCommonConfig.class)
public class SpringBeanTest {// 这个mock对象会注入Spring容器@MockBeanprivate SampleRepository sampleRepository1;// 真实调用该对象逻辑@SpyBeanprivate SampleRepository sampleRepository2;@Autowiredprivate SampleRepository sampleRepository3;@Autowiredprivate ApplicationContext applicationContext;@Autowiredprivate SampleConfig sampleConfig;@Testpublic void sampleBeanTest() throws JSONException {SampleRepository bean = applicationContext.getBean(SampleRepository.class);Assert.assertEquals(sampleRepository1,bean);}}

此外我们使用h2内存数据库达到对Mapper的测试,也有testcontainers库推出用于测试与外部系统的交互,这里不赘述,详见示例代码

相关文章:

  • Spark SQL 血缘解析方案
  • 【Apache Doris】周FAQ集锦:第 7 期
  • python,ipython 和 jupyter notebook 之间的关系
  • 什么是N卡和A卡?有什么区别?
  • Python设计模式 - 简单工厂模式
  • Linux驱动开发笔记(十一)tty子系统及其驱动
  • AMSR/ADEOS-II L1A Raw Observation Counts V003地球表面和大气微波辐射的详细观测数据
  • 计算机组成原理笔记-第1章 计算机系统概论
  • 大疆无人机航点飞行KMZ文件提取航点坐标
  • 保存和调取得分的简易方法
  • Github 2024-06-19 C开源项目日报 Top9
  • C#面: 能够将非静态的方法覆写成静态方法吗?
  • Jenkins macos 下 failed to create dmg 操作不被允许hdiutil: create failed - 操作不被允许?
  • 使用Redis优化Java应用的性能
  • 如何将 ChatGPT 集成到你的应用中
  • 【翻译】babel对TC39装饰器草案的实现
  • Android框架之Volley
  • Apache的80端口被占用以及访问时报错403
  • ECS应用管理最佳实践
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • FineReport中如何实现自动滚屏效果
  • HomeBrew常规使用教程
  • IDEA 插件开发入门教程
  • iOS 颜色设置看我就够了
  • Java 实战开发之spring、logback配置及chrome开发神器(六)
  • PaddlePaddle-GitHub的正确打开姿势
  • SegmentFault 2015 Top Rank
  • 百度地图API标注+时间轴组件
  • 多线程 start 和 run 方法到底有什么区别?
  • 工程优化暨babel升级小记
  • 关于 Linux 进程的 UID、EUID、GID 和 EGID
  • 聚类分析——Kmeans
  • 前嗅ForeSpider教程:创建模板
  • 浅谈JavaScript的面向对象和它的封装、继承、多态
  • “十年磨一剑”--有赞的HBase平台实践和应用之路 ...
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • ​html.parser --- 简单的 HTML 和 XHTML 解析器​
  • #includecmath
  • (3)nginx 配置(nginx.conf)
  • (ros//EnvironmentVariables)ros环境变量
  • (二)pulsar安装在独立的docker中,python测试
  • (含笔试题)深度解析数据在内存中的存储
  • (十二)Flink Table API
  • (五)c52学习之旅-静态数码管
  • (一)项目实践-利用Appdesigner制作目标跟踪仿真软件
  • (转)使用VMware vSphere标准交换机设置网络连接
  • (最全解法)输入一个整数,输出该数二进制表示中1的个数。
  • .htaccess配置重写url引擎
  • .NET 4.0网络开发入门之旅-- 我在“网” 中央(下)
  • .NET 6 Mysql Canal (CDC 增量同步,捕获变更数据) 案例版
  • .net web项目 调用webService
  • .NET 设计模式—适配器模式(Adapter Pattern)
  • .NET/C# 获取一个正在运行的进程的命令行参数
  • .NET/C# 在代码中测量代码执行耗时的建议(比较系统性能计数器和系统时间)...
  • .Net调用Java编写的WebServices返回值为Null的解决方法(SoapUI工具测试有返回值)