1 配置单元测试环境及简单使用
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
- 配置相关注解
@RunWith(SpringRunner.class) //底层⽤junit SpringJUnit4ClassRunner
@SpringBootTest(classes={Springbootdemo2Application.class})//启动整个springboot⼯程
class Springbootdemo2ApplicationTests {
- 常⽤单元测试的注解
@Before
void before() {
System.out.println("Before...");
}
@Test
void test1() {
System.out.println("test1...");
}
@After
void after() {
System.out.println("After...");
}
- 断⾔:判断程序结果是否符合预期
@Test
void test1() {
TestCase.assertEquals(0, 1);
System.out.println("test1...");
}
@Test
void test2() {
TestCase.assertEquals(0, 0);
System.out.println("test2...");
}
2 Controller-Service层接⼝测试
2.1 Controller层登录⽅法测试
- 注入controller
class Springbootdemo2ApplicationTests {
@Autowired
private UserController userController;
...
- 编写测试方法
@Test
void loginTest() {
User user = new User();
user.setUsername("jackyan");
user.setPassword("123456");
RetData retData = userController.login(user);
System.out.println(retData.toString());
TestCase.assertEquals(0, retData.getCode());
}
- 测试结果
2.2 视频列表Service层单元测试
- 注入service
@Autowired
private VideoService videoService;
- 编写测试方法
@Test
void videoListTest() {
List<Video> videoList = videoService.listVideo();
TestCase.assertTrue(videoList.size()>0);
}
- 测试结果:测试通过
3 MockMvc调用controller层API
使用MockMvc测试controller层对外提供的接口
3.1 使用方法
- 增加类注解
@AutoConfigureMockMvc
- 注⼊⼀个MockMvc类
@Autowired
private MockMvc mockMvc;
- 相关API
perform执⾏⼀个RequestBuilder请求
andExpect:添加ResultMatcher->MockMvcResultMatchers验证规则
andReturn:最后返回相应的MvcResult->Response - 示例:VideoController 视频列表接⼝单元测试
@AutoConfigureMockMvc
@RunWith(SpringRunner.class) //底层⽤junit SpringJUnit4ClassRunner
@SpringBootTest(classes={Springbootdemo2Application.class})//启动整个springboot⼯程
class Springbootdemo2ApplicationTests {
@Autowired
private MockMvc mockMvc;
@Before
void before() {
System.out.println("Before...");
}
/**
* 测试对外接口
*/
@Test
void testVideoListApi() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/app/v1/video/list"))
.andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
int status = mvcResult.getResponse().getStatus();
System.out.println(status);
String contentAsString = mvcResult.getResponse().getContentAsString(Charset.forName("UTF-8"));
System.out.println(contentAsString);
}
@After
void after() {
System.out.println("After...");
}
}
- 测试结果:正常获取数据
200
{"code":0,"data":[{"id":1,"title":"东邪西毒","summary":null,"price":null,"cover_img":null,"create_time":"2022-01-28 02:12:25","chapter_list":null},{"id":2,"title":"精武英雄","summary":null,"price":null,"cover_img":null,"create_time":"2022-01-28 02:12:25","chapter_list":null},{"id":3,"title":"霸王别姬","summary":null,"price":null,"cover_img":null,"create_time":"2022-01-28 02:12:25","chapter_list":null},{"id":4,"title":"南极大冒险","summary":null,"price":null,"cover_img":null,"create_time":"2022-01-28 02:12:25","chapter_list":null},{"id":5,"title":"变形金刚","summary":null,"price":null,"cover_img":null,"create_time":"2022-01-28 02:12:25","chapter_list":null},{"id":6,"title":"流浪地球","summary":null,"price":null,"cover_img":null,"create_time":"2022-01-28 02:12:25","chapter_list":null}],"message":"success"}