测试所需依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
说明
@RunWith注解:SpringRunner.class使用 Junit 整合 Spring
MockMvc: 注入 MockMvc 模拟 Http 请求
@Before注解:在@Test运行前调用进行预先配置,相关的注解流程是(@BeforeClass->@Before->@Test->@After->@AfterClass)
@WebAppConfiguration: Web项目,Junit需要模拟ServletContext,测试类加上@WebAppConfiguration。
package eugene.project.eop.UserAbout;
import eugene.project.eop.model.EProject;
import eugene.project.eop.model.EUser;
import eugene.project.eop.repository.EProjectRepository;
import eugene.project.eop.repository.EUserRepository;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
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 org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import javax.annotation.Resource;
import java.util.List;
@SpringBootTest
@RunWith(SpringRunner.class)
@WebAppConfiguration
public class FunctionTest {
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Resource
private EUserRepository userRepository;
@Resource
private EProjectRepository projectRepository;
@Before
public void setup() throws Exception {
this.mockMvc = MockMvcBuilders.webAppContextSetup(this.webApplicationContext).build();
}
@Test
public void testApi() throws Exception {
JSONObject jsonObject = new JSONObject();
jsonObject.put("phone", "13700000000");
jsonObject.put("password", "1");
String requestContent = jsonObject.toString();
MockHttpServletRequestBuilder request = MockMvcRequestBuilders
.post("/login")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(requestContent);
MvcResult result = mockMvc.perform(request)
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
System.out.println("result : " + result.getResponse().getContentAsString());
}
}
测试结果
Hibernate: select euser0_.id as id1_3_, euser0_.binding_phone as binding_2_3_, euser0_.password as password3_3_, euser0_.username as username4_3_ from user euser0_ where euser0_.binding_phone=?
MD5验证通过
result : {"msg":"success","data":{"token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI5In0._orNWXJE5wEcHqL-2LKBgKGKj9UbUCEWFb_eiOyzyPQ"},"status":200}