spring boot:为service类做参数化单元测试(spring boot 2.4.3)
2023-09-14 08:59:32 时间
一,演示项目的相关信息
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 Boot应用
- 【Spring Boot 文档翻译】文档简介
- Spring Boot启动时数据库初始化spring.datasource(转)
- Spring Boot 2 快速教程:WebFlux Restful CRUD 实践(三)
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
- Spring的JDBC
- [Java Spring Data] Optional<> query response, orElse, orElseThrow, ifPresent
- spring boot单元测试之八:用mockmvc模拟header参数(spring boot 2.4.4)
- spring boot单元测试之四:单元测试中测试文件上传(spring boot 2.4.3)
- spring boot:thymeleaf给fragment传递参数的方法(spring boot 2.3.3)
- spring boot:用cookie保存i18n信息避免每次请求时传递参数(spring boot 2.3.3)
- spring boot: 用thymeleaf嵌套循环展示多层数据(spring boot 2.3.2)
- spring 学习3-Spring AOP
- spring boot json参数调用
- spring boot连接mysql8.0
- Spring Boot——2分钟构建spring web mvc REST风格HelloWorld
- spring boot:接收数组参数及多文件混合json参数(spring boot 2.3.4)
- spring boot:用swagger3生成接口文档,支持全局通用参数(swagger 3.0.0 / spring boot 2.3.2)
- spring boot:使用validator做接口的参数、表单、类中多字段的参数验证(spring boot 2.3.1)
- Spring Boot项目开启异步使用多线程完整代码案例
- 学习Spring Boot:(十四)spring-shiro的密码加密
- spring boot配置dubbo注意事项
- spring boot --- 注解 @Bean 和@Component
- 【Spring注解驱动开发】你敢信?面试官竟然让我现场搭建一个AOP测试环境!