<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
在test包下写测试类便可。git
@RunWith(SpringRunner.class) @SpringBootTest @WebAppConfiguration @AutoConfigureMockMvc public abstract class BaseTest { @Autowired protected MockMvc mvc; }
其余类继承 BastTest类 便可。github
public class DemoTest extends BaseTest{ @Autowired private StorehouseService storehouseService; //建立仓库信息 @Test public void testAddStorehouseController() throws Exception { Storehouse sh = new Storehouse(); sh.setName("ssssssssss"); sh.setCityid(234); sh.setBranchid(17); sh.setDetails("ooooooo"); ObjectMapper mapper = new ObjectMapper(); mvc.perform(MockMvcRequestBuilders.post("/storehouse") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(mapper.writeValueAsString(sh))) .andExpect(MockMvcResultMatchers.status().isOk()) // {"code":200,"message":"ok","data":{"content":[{"id":1,"loginname":"admin","name":"admin","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":0,"status":0,"organizationid":1,"createtime":1449378845000},{"id":13,"loginname":"snoopy","name":"snoopy","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":3,"createtime":1443676327000},{"id":14,"loginname":"dreamlu","name":"dreamlu","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":5,"createtime":1444576378000},{"id":15,"loginname":"test","name":"test","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":6,"createtime":1449378783000}],"last":true,"totalPages":1,"totalElements":4,"number":0,"size":10,"numberOfElements":4,"first":true,"sort":null}} .andExpect(MockMvcResultMatchers.jsonPath("code", Is.is(200))) .andExpect(MockMvcResultMatchers.jsonPath("data", IsMapContaining.hasEntry("name","ssssssssss"))) .andDo(MockMvcResultHandlers.print()) .andReturn(); //最后删除数据 String query = "cityid:234,branchid:17"; Page<Storehouse> storehouse = storehouseService.getAllStorehouse(query, null, 0, 1); Storehouse storeh = storehouse.getContent().get(0); storehouseService.deleteStorehouse(storeh); } /* * //更新仓库信息 @Test public void testUpdateStorehouseController() throws Exception { Storehouse sh = new Storehouse(); //根据条件查询出id String query = "cityid:234,branchid:17"; Page<Storehouse> storehouse = storehouseService.getAllStorehouse(query, null, 0, 1); Storehouse storeh = storehouse.getContent().get(0); Integer id = storeh.getId(); //将id放入storehouse中 sh.setId(id); sh.setName("ssssssssss"); sh.setCityid(234); sh.setBranchid(18); sh.setDetails("ooooooo"); ObjectMapper mapper = new ObjectMapper(); mvc.perform(MockMvcRequestBuilders.put("/storehouse") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(mapper.writeValueAsString(sh))) .andExpect(MockMvcResultMatchers.status().isOk()) // {"code":200,"message":"ok","data":{"content":[{"id":1,"loginname":"admin","name":"admin","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":0,"status":0,"organizationid":1,"createtime":1449378845000},{"id":13,"loginname":"snoopy","name":"snoopy","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":3,"createtime":1443676327000},{"id":14,"loginname":"dreamlu","name":"dreamlu","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":5,"createtime":1444576378000},{"id":15,"loginname":"test","name":"test","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":6,"createtime":1449378783000}],"last":true,"totalPages":1,"totalElements":4,"number":0,"size":10,"numberOfElements":4,"first":true,"sort":null}} .andExpect(MockMvcResultMatchers.jsonPath("code", Is.is(200))) .andExpect(MockMvcResultMatchers.jsonPath("data", IsMapContaining.hasEntry("branchid",18))) .andDo(MockMvcResultHandlers.print()) .andReturn(); } */ //根据仓库id获取仓库信息 @Test public void testGetStorehouseController() throws Exception { //创造数据 Storehouse sh = new Storehouse(); sh.setName("ssssssssss"); sh.setCityid(235); sh.setBranchid(17); sh.setDetails("ooooooo"); storehouseService.addStorehouse(sh); //先根据条件查询出id String query = "cityid:235,branchid:17"; Page<Storehouse> storehouse = storehouseService.getAllStorehouse(query, null, 0, 1); Storehouse storeh = storehouse.getContent().get(0); Integer id = storeh.getId(); //开始单元测试 mvc.perform(MockMvcRequestBuilders.get("/storehouse/"+id) .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(MockMvcResultMatchers.status().isOk()) // {"code":200,"message":"ok","data":{"content":[{"id":1,"loginname":"admin","name":"admin","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":0,"status":0,"organizationid":1,"createtime":1449378845000},{"id":13,"loginname":"snoopy","name":"snoopy","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":3,"createtime":1443676327000},{"id":14,"loginname":"dreamlu","name":"dreamlu","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":5,"createtime":1444576378000},{"id":15,"loginname":"test","name":"test","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":6,"createtime":1449378783000}],"last":true,"totalPages":1,"totalElements":4,"number":0,"size":10,"numberOfElements":4,"first":true,"sort":null}} .andExpect(MockMvcResultMatchers.jsonPath("code", Is.is(200))) .andExpect(MockMvcResultMatchers.jsonPath("data", IsMapContaining.hasEntry("name","ssssssssss"))) .andDo(MockMvcResultHandlers.print()) .andReturn(); //最后删除数据 storehouseService.deleteStorehouse(sh); } //根据条件获取仓库信息列表 @Test public void testGetAllController() throws Exception { //创造数据 Storehouse sh = new Storehouse(); sh.setName("ssssssssssl"); sh.setCityid(236); sh.setBranchid(17); sh.setDetails("ooooooo"); storehouseService.addStorehouse(sh); //根据条件查询出id String query = "cityid:236,branchid:17"; ObjectMapper mapper = new ObjectMapper(); MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>(); params.set("query",query); params.set("sortby",null); params.set("page","0"); params.set("pagesize","10"); mvc.perform(MockMvcRequestBuilders.get("/storehouse") .accept(MediaType.APPLICATION_JSON_VALUE) .contentType(MediaType.APPLICATION_JSON_UTF8) .params(params)) // .content(mapper.writeValueAsString(null)) // .content(mapper.writeValueAsString(null)) // .content(mapper.writeValueAsString(null))) .andExpect(MockMvcResultMatchers.status().isOk()) // {"code":200,"message":"ok","data":{"content":[{"id":1,"loginname":"admin","name":"admin","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":0,"status":0,"organizationid":1,"createtime":1449378845000},{"id":13,"loginname":"snoopy","name":"snoopy","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":3,"createtime":1443676327000},{"id":14,"loginname":"dreamlu","name":"dreamlu","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":5,"createtime":1444576378000},{"id":15,"loginname":"test","name":"test","password":"098f6bcd4621d373cade4e832627b4f6","sex":0,"age":25,"phone":"18707173376","usertype":1,"status":0,"organizationid":6,"createtime":1449378783000}],"last":true,"totalPages":1,"totalElements":4,"number":0,"size":10,"numberOfElements":4,"first":true,"sort":null}} .andExpect(MockMvcResultMatchers.jsonPath("code", Is.is(200))) .andExpect(MockMvcResultMatchers.jsonPath("$.data.content[0]", IsMapContaining.hasEntry("name","ssssssssssl"))) .andDo(MockMvcResultHandlers.print()) .andReturn(); //最后删除数据 storehouseService.deleteStorehouse(sh); } }
一、此测试类中的每个单元测试方法,测试前若是须要数据,会本身创造数据,测试完后会本身删除数 据,不会对数据库留下痕迹。spring
二、注意小细节:数据库
三、test类的名字必须以 *Test 结尾。json
jsonPath: https://github.com/jayway/jsonPath(转载)mvc
须要使用的判断校验的工具类包:app