简介
spring-boot-starter-test 组成包括
- JUnit — The de-facto standard for unit testing Java applications.
- Spring Test & Spring Boot Test — Utilities and integration test support for Spring Boot applications.
- AssertJ — A fluent assertion library.
- Hamcrest — A library of matcher objects (also known as constraints or predicates).
- Mockito — A Java mocking framework.
- JSONassert — An assertion library for JSON.
- JsonPath — XPath for JSON.
这里讲的是 junit5 + mock
单元测试
引入 spring-boot-starter-test
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
依赖还包括 web
编写接口
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;
/**
* 测试前段与后端之间交互
*/
@RestController
@RequestMapping("/test/param")
public class TestParamController {
/**
* req
* http://localhost:8011/test/param/post/simple
* <p>
* form-data
* id f34yfhgf
* <p>
* response
* {
* "code": "200",
* "id": "f34yfhgf"
* }
*/
@RequestMapping("/post/simple")
public Map<String, String> simple(String id) {
return new HashMap<String, String>() {{
put("code", "200");
put("id", id);
}};
}
}
启动springboot 容器测试
@SpringBootTest
class SpringBootLearnApplicationTests {
@Test
void contextLoads() {
System.out.println("contextLoadsd");
}
}
启动springboot容器 并且调用接口测试
package cn.gd.cz.hong.springbootlearn.controller;
import org.assertj.core.api.Assert;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.event.annotation.BeforeTestClass;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.junit.jupiter.api.Assertions.*;
//SpringBootTest 是springboot 用于测试的注解,可指定启动类或者测试环境等,这里直接默认。
//因为是mock测试,在实际开发过程中,可指定其测试启动时为随机端口,避免了不必要的端口冲突。
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//测试单一接口时 ,也可利用注解@WebMvcTest 进行单一测试
//@WebMvcTest(DemoController.class)
class TestParamControllerTest {
//使用 WebMvcTest 时使用
//@autowired mockMvc 是可自动注入的。
//当直接使用SpringBootTest 会提示 注入失败 这里直接示例利用 MockMvcBuilders工具创建
//@Autowired
MockMvc mockMvc;
@Autowired
WebApplicationContext wc;
@BeforeEach
public void beforeSetUp() {
this.mockMvc = MockMvcBuilders.webAppContextSetup(wc).build();
}
@Test
void simple() throws Exception {
String id = "344698773532";
MvcResult result = this.mockMvc
.perform(MockMvcRequestBuilders.get("/test/param/post/simple").param("id", id))
.andDo(MockMvcResultHandlers.print()).andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
//断言 是否和预期相等
System.out.println(result.getResponse().getContentAsString());
}
}
坑爹玩意
junit4 如果需要测试容器
除了要加上@SpringBootTest之外 还需要加上@RunWith
到了junit5之后@RunWith可以不需要加上
参考

网友评论