spring boot 基于Controller自动化测试demo

测试包:

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

项目目录:

在test包下写测试类即可。

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

其他类继承 BastTest类 即可。

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);
    }

}

注意事项:

1、此测试类中的每一个单元测试方法,测试前如果需要数据,会自己创造数据,测试完后会自己删除数            据,不会对数据库留下痕迹。

2、注意小细节:

3、test类的名字必须以   *Test  结尾。

jsonPath:    https://github.com/jayway/jsonPath(转载)

需要使用的判断校验的工具类包:

 

 

转载于:https://my.oschina.net/u/2356966/blog/834661

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值