0%
引入依赖包
pom.xml1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
|
创建测试目录,如下图
Service 层单元测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| package org.example;
import org.example.po.User; import org.example.service.UserService; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource; import javax.swing.*;
@RunWith(SpringRunner.class) @SpringBootTest(classes = Starter.class) public class TestUserService {
@Resource private UserService userService;
@Before public void before(){ System.out.println("单元测试之前执行..."); }
@After public void after(){ System.out.println("单元测试之后执行..."); }
@Test public void testFindUserById(){ User user = userService.findUserById(1); System.out.println(user); } }
|
控制层接口方法测试
视图层代码使用 MockMvc 进行测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
| package org.example;
import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.context.junit4.SpringRunner; 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.MockHttpServletRequestBuilder; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import javax.annotation.Resource;
@RunWith(SpringRunner.class) @SpringBootTest(classes = {Starter.class}) @AutoConfigureMockMvc public class TestUserController { @Resource private MockMvc mockMvc;
@Test public void testFindUserById() throws Exception { MockHttpServletRequestBuilder request = MockMvcRequestBuilders.get("/users/1/id") .contentType("text/html") .accept(MediaType.APPLICATION_JSON) .characterEncoding("UTF-8");
ResultActions perform = mockMvc.perform(request);
perform.andExpect(MockMvcResultMatchers.status().isOk());
MvcResult result = perform.andReturn();
MockHttpServletResponse response = result.getResponse();
System.out.println("状态码:" + response.getStatus()); System.out.println("响应数据:" + response.getContentAsString()); }
@Test public void testFindUserById2() throws Exception { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/users/1/id")) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn();
System.out.println("状态码:" + mvcResult.getResponse().getStatus()); System.out.println("响应数据:" + mvcResult.getResponse().getContentAsString()); } }
|