spring boot 基於Controller自動化測試demo

測試包:

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
</dependency>

項目目錄:

在test包下寫測試類便可。git

BastTest類

@RunWith(SpringRunner.class)
@SpringBootTest
@WebAppConfiguration
@AutoConfigureMockMvc
public abstract class BaseTest {
   @Autowired
   protected MockMvc mvc;
}

DemoTest類

其餘類繼承 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

相關文章
相關標籤/搜索