spring boot:为service类做参数化单元测试(spring boot 2.4.3)
2023-09-14 09:01:18 时间
一,演示项目的相关信息
1,地址:
https://github.com/liuhongdi/servicetest
2,功能说明:演示给一个service生成测试文件
3,项目结构:如图:
说明:刘宏缔的架构森林是一个专注架构的博客,地址:https://www.cnblogs.com/architectforest
对应的源码可以访问这里获取: https://github.com/liuhongdi/
说明:作者:刘宏缔 邮箱: 371125307@qq.com
二,如何创建一个测试文件?
1,可以手动创建一个class文件
2,在intellij idea中可以快速创建一个test文件:
打开一个service类,在类名上执行: ctrl+shift+T
然后从弹出的窗口中选择:
Create New Test
如图:
三,java代码说明
1,service/HelloService.java
package com.servicetest.demo.service; import org.springframework.stereotype.Service; @Service public class HelloService { public String sayHello(String name) { return "您好,"+name+"!"; } public int addTwo(int first,int second) { return first+second; } }
2,service/HelloServiceTest.java
package com.servicetest.demo.service; import org.junit.jupiter.api.*; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import org.springframework.boot.test.context.SpringBootTest; import javax.annotation.Resource; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest @DisplayName("测试HelloService") class HelloServiceTest { @Resource HelloService helloService; @BeforeAll static void initAll() { System.out.println("this is BeforeAll"); } @AfterAll static void endAll() { System.out.println("this is AfterAll"); } @BeforeEach public void initOne() { System.out.println("this is BeforeEach"); } @AfterEach public void endOne() { System.out.println("this is AfterEach"); } @Test @DisplayName("测试sayhello方法") void sayHello() { String hellostr = helloService.sayHello("老刘"); assertThat(hellostr, equalTo("您好,老刘!")); assertThat(hellostr, hasLength(6)); assertThat(hellostr, containsString("您好,")); } @ParameterizedTest @CsvSource({"1,2,3", "11,12,23", "33,100,133"}) @DisplayName("参数化测试") void addTwo(int first,int second,int sum) { int rest = helloService.addTwo(first,second); assertThat(rest, equalTo(sum)); } }
四,测试效果
五,查看spring boot的版本:
. ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.4.3)
相关文章
- Spring 全家桶之 Spring Boot 2.6.4(十)- 自定义Starter
- java、spring线程池面试题
- spring的配置文件-applicationContext.xml
- Spring框架:第七章:AOP切面编程
- spring boot项目读取配置文件的参数
- Spring Boot整合OpenOffice实现Word、Excel、PPT在线预览
- springboot 集成mybatis-plus_Spring Boot
- 烧脑巨作,Spring Boot请求处理(常用参数注解使用)
- Spring循环依赖-spring源码详解(四)
- Elastic-Job2.1.5源码-自定义Spring标签与Spring 依赖注入无缝整合
- Spring Boot 3.0 正式发布,王炸!!
- 导致Spring事务失效的原因有哪些?
- Spring自带工具类(断言、ObjectUtils、FileCopyUtils、ResourceUtils、StreamUtils、ReflectionUtils、AopUtils、AopCont)
- Spring Boot 3.0横空出世,快来看看是不是该升级了
- Spring Boot的加密和解密(二)
- Spring Boot的测试框架(二)
- Hystrix和Spring Boot的整合
- Mongodb3.0.5 副本集搭建及spring和java连接副本集配置详细介绍
- 一文读懂 Spring Boot、微服务架构和大数据治理三者之间的故事详解大数据
- Spring之AOP Annotation方式详解编程语言
- Spring整合ActiveMQ及多个Queue消息监听的配置详解编程语言
- Spring Boot全局支持CORS(跨源请求)的配置方法详解编程语言
- Spring Boot核心配置详解编程语言
- Spring Boot发布将jar包转为war包。详解编程语言
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控详解编程语言
- Spring Boot(十六):使用Jenkins部署Spring Boot详解编程语言
- Spring Boot(十五):spring boot+jpa+thymeleaf增删改查示例详解编程语言
- Spring Boot(十四):spring boot整合shiro-登录认证和权限管理详解编程语言
- Spring boot 定时器详解编程语言
- Spring整合Redis简单实现高效缓存(spring集成redis)
- 使用Spring集成Redis,实现全新的数据存储方案(sprng集成redis)