java单元测试
添加依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
接口单元测试(get和post两种方式)
@RunWith(SpringRunner.class) @SpringBootTest public class RoleControllerTest extends BaseControllerTest { @Test public void pageRole(){ try { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/gwls/comalarm/role/pageRole") //.contentType(MediaType.APPLICATION_JSON_UTF8) .param("offset", "1") .param("limit","100") .param("sort","") .param("order","") .param("search","") //.content(JSONUtil.toJson(dictionary)) .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json .andReturn();// 得到返回结果 MockHttpServletResponse response = mvcResult.getResponse(); //拿到请求返回码 int status = response.getStatus(); //拿到结果 String contentAsString = response.getContentAsString(); System.err.println(status); System.err.println(contentAsString); } catch (Exception e) { e.printStackTrace(); } } @Test public void addRole(){ try { Role role = new Role(); role.setRoleName("hcj测试"); role.setPost(1); role.setCompanyId(692); MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/gwls/comalarm/role/addRole") .contentType(MediaType.APPLICATION_JSON_UTF8) .param("roleJson", JSONUtil.toJson(role)) .param("menuIds","17,18,19") // .param("deviceIds","1,2,3") .param("regionAndDeviceIds","4_296,7") .param("homePageId","113") //.content(JSONUtil.toJson(dictionary)) .accept(MediaType.APPLICATION_JSON)) // 断言返回结果是json .andReturn();// 得到返回结果 MockHttpServletResponse response = mvcResult.getResponse(); //拿到请求返回码 int status = response.getStatus(); //拿到结果 String contentAsString = response.getContentAsString(); System.err.println(status); System.err.println(contentAsString); } catch (Exception e) { e.printStackTrace(); } } }
方法单元测试
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = SystemApplication.class) public class RoleServiceTest { @Autowired private RoleService roleService; @Test public void getRole(){ roleService.getRole(5); } }
业精于勤,大家加油鸭

浙公网安备 33010602011771号