Spring Boot 集成测试
一、测试一般程序
1.1 测试步骤
- 在
pom.xml
中加入测试环境的依赖。 - 在测试类上加入
@RunWith(SpringRunner.class)
与@SpringBootTest
注解。
1.2 简单测试例子
在pom.xml
中引入相关依赖
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>
要测试的程序代码
@Repository
public class UserDao {public void addUser(String username){if(username.equals("zhangsan")){System.out.println("===============");}else {System.out.println("---------------");}}
}
测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootApplicationTests {@Autowiredprivate UserDao userDao;@Testpublic void testAddUser() {userDao.addUser("zhangsan");}}
测试结果
二、测试 Controller
2.1 使用TestRestTemplate
对象测试
测试步骤
- 在
pom.xml
中加入测试环境的依赖。 - 在测试类上加入
@RunWith(SpringRunner.class)
与
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
注解。 - 使用
TestRestTemplate
完成测试
要测试的 controller
@RestController
public class UserController{@GetMapping("/show/{id}")public String show(@PathVariable Integer id){return "show" + id; }}
测试代码
/** TestRestTemplate 需要运行在 web 项目中 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class ControllerTest {@Autowiredprivate TestRestTemplate template;@Testpublic void testController(){// template.getForObject() 会得到 controller 返回的 json 值String content = template.getForObject("/show/100", String.class);// 使用断言测试,使用正确的断言Assert.assertEquals("show100", content);}
}
正确的测试结果
当断言不正确时,测试的结果
2.2 使用@WebMvcTest
注解测试
测试步骤
- 在
pom.xml
中加入测试环境的依赖。 - 在测试类上加入
@RunWith(SpringRunner.class)
与@WebMvcTest
注解。
仍然使用UserController
控制器。
测试代码
/** @WebMvcTest 注解需要指定测试控制器所在的类 */
@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class ControllerTest2 {@Autowiredprivate MockMvc mvc;@Testpublic void testController() throws Exception {// 模拟请求,并期望执行成功mvc.perform(MockMvcRequestBuilders.get("/show/100").param("id", "100")).andExpect(MockMvcResultMatchers.status().isOk());// 模拟请求,并期望执行成功,以及期望其返回的值是“show100”mvc.perform(MockMvcRequestBuilders.get("/show/100").param("id", "100")).andExpect(MockMvcResultMatchers.content().string("show100"));}
}
正确的测试结果
当期望返回的结果与请求的数据不一致时,测试的结果
2.3@WebMvcTest
与@SpringBootTest
注解总结
@WebMvcTest
与@SpringBootTest
注解不能在一起使用。
还需要注意的地方是在使用@WebMvcTest
注解进行测试时,该注解不会加载在 controller 中的其他依赖。也就是说这个注解不会加载整个 Spring 容器,它只会加载在@WebMvcTest()
中配置的 bean。
@SpringBootTest
注解会加载所有被 Spring 容器管理的 bean。
MockMvc
对象并不常常与@WebMvcTest()
注解在一起使用,在你想使用MockMvc
对象时,又希望会加载被 Spring 容器管理的 bean,你可以这样做。
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
public class TestController3 {@Autowiredprivate MockMvc mockMvc;
}
Spring Boot 集成测试相关推荐
- selenium持续集成_使用Selenium进行Spring Boot集成测试
selenium持续集成 Web集成测试允许对Spring Boot应用程序进行集成测试,而无需进行任何模拟. 通过使用@WebIntegrationTest和@SpringApplicationCo ...
- Spring Boot集成测试中@ContextConfiguration和@SpringApplicationConfiguration之间的区别
即使同时使用@ContextConfiguration和@SpringApplicationConfiguration批注以及SpringJUnit4ClassRunner来指定如何加载Spring应 ...
- 使用Selenium进行Spring Boot集成测试
Web集成测试允许对Spring Boot应用程序进行集成测试,而无需进行任何模拟. 通过使用@WebIntegrationTest和@SpringApplicationConfiguration我们 ...
- 不要在 Spring Boot 集成测试中使用 @Transactional
在测试运行时,测试类中 @Transactional 注解,会导致测试中 Entity 数据的操作都是在内存中完成,最终并不会进行 commit 操作,也就是不会将 Entity 数据进行持久化操作, ...
- boot jersey_Jersey和Spring Boot入门
boot jersey 除了许多新功能,Spring Boot 1.2还带来了Jersey支持. 这是吸引喜欢标准方法的开发人员的重要一步,因为他们现在可以使用JAX-RS规范构建RESTful AP ...
- 使用Testcontainers和PostgreSQL,MySQL或MariaDB的Spring Boot测试
Testcontainers是一个Java库,可轻松将Docker容器集成到JUnit测试中. 在Containerized World中 ,将测试配置与嵌入式数据库和服务复杂化几乎没有意义. 而是使 ...
- Jersey和Spring Boot入门
除了许多新功能,Spring Boot 1.2还带来了Jersey支持. 这是吸引喜欢标准方法的开发人员的重要一步,因为他们现在可以使用JAX-RS规范构建RESTful API,并将其轻松部署到To ...
- Spring Boot 的单元测试和集成测试
点击蓝色"程序猿DD"关注我 回复"资源"获取独家整理的学习资料! 作者 | 万想 来源 | 公众号「锅外的大佬」 学习如何使用本教程中提供的工具,并在 Spr ...
- 在Spring Boot测试中使用Testcontainer进行数据库集成测试
在此博客文章中,我想演示如何在Spring Boot测试中集成Testcontainer以便与数据库一起运行集成测试. 我没有使用Testcontainers的Spring Boot模块. 如何与他们 ...
最新文章
- 近期活动盘点:2019清华大数据系统软件峰会(9.15)
- 漫画:如何在数组中找到和为 “特定值” 的两个数?
- Java面试题:缓存三大问题及解决方案!
- 一道Java面试题目——随想+心得
- 科技感的动态设计方法-1
- qt cef嵌入web(二)
- 理解并实施:VRRP(200-120新增考点)
- 处理api返回的数据_API 乐队指挥家,网关服务正式上线
- spring rmi_Spring远程支持和开发RMI服务
- 再送一波干货,测试2000线程并发下同时查询1000万条数据库表及索引优化
- 雷军谈小米10售价:不贵,交个朋友;百度开源首个口罩人脸检测模型;优麒麟18.04.4 LTS发布 | 极客头条...
- 广告传媒实际税负怎么计算_传媒宣传业增值税税负多少
- proguard-gui 混淆代码简要笔记
- lopatkin俄大神Windows精简版系统安装注意事项及恢复OneDrive
- 波士顿房价数据集——随机森林
- B.系数(mod意义下的系数转化+lucas)
- Windows权限维持方法
- PHP 家长互助解决问题步骤,小区父母互助 完善自己做合格家长
- Android-app内部调整字体大小
- java面试宝典2017