WEB端自测的三种方法比较

1.案例

使用的url如:

"http://localhost:8080/e/invoice/config/register/queryRegisterStatus?shopId=23591270"

 

2.比较

方法

优点

缺点

备注

使用postman

不用写代码

没法跟着项目代码走,容易丢失

 

使用springMockMVC

像单测一样使用

web项目每次都需要启动,启动耗时较长

 

使用HttpClient

像单测一样使用

web项目需要启动着

 

 

考虑可以结合使用,临时测试时使用postman,集成测试时使用httpClient。

 

3.使用

  • 使用postman访问

  • 使用springMockMvc

    @Test
    public void queryRegisterStatusTest1() throws Exception {
        int shopId = 23591270;
        InvoiceAccountBO accountBO = getAccount(shopId);

        this.mockMvc
                .perform(get("/e/invoice/config/register/queryRegisterStatus?shopId="+shopId)
                        .accept(MediaType.APPLICATION_JSON)
                        .requestAttr(Constant.INVOICE_ACCOUNT_BO,accountBO))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().contentType("application/json;charset=UTF-8"));

        System.out.println("end");
    }
  • 使用httpclient

@Test
    public void queryRegisterStatusTest() {

        int shopId = 23591270;

        try {
            CloseableHttpClient client = null;
            CloseableHttpResponse response = null;
            try {
                HttpGet httpGet = new HttpGet(uri + "/e/invoice/config/register/queryRegisterStatus?shopId="+shopId);

                client = HttpClients.createDefault();
                response = client.execute(httpGet);
                HttpEntity entity = response.getEntity();

                String result = JSON.toJSONString(JSONObject.parseObject(EntityUtils.toString(entity)),true);
                System.out.println("result:"+result);

            } finally {
                if (response != null) {
                    response.close();
                }
                if (client != null) {
                    client.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值