欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring Boot 集成测试

发布时间:2024/9/30 javascript 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 集成测试的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。