Api数据库断言(充值接口)

2020-05-10  本文已影响0人  testerPM

RechargeCase .java代码如下:


package com.lemon.cases;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONPath;
import com.lemon.pojo.Api;
import com.lemon.pojo.Cases;
import com.lemon.pojo.UseJsonPathAssert;
import com.lemon.utils.DataUtils;
import com.lemon.utils.HttpUtils;
import com.lemon.utils.SQLUtils;

public class RechargeCase extends BaseCase {

    @Test(dataProvider = "datas")
    public void testRecharge(Api api, Cases cases) {
        //数据库前置查询结果
        String  sql=cases.getSql();
        Object BeforeMoney=SQLUtils.Query(sql);
        
        // 获取响应体
        String actualResponseBody = ActualResponseBody(api, cases);// 子类调用父类方法
        // 添加回写
        addWriteBackData(cases, actualResponseBody);
        boolean assertResponseData = AssertResponseData(cases, actualResponseBody);
        System.out.println("响应数据断言结果:"+assertResponseData);
        //数据库后置查询结果
        Object  AfterMoney=SQLUtils.Query(sql);
        if(StringUtils.isNoneBlank(sql)){
            //数据库断言
            boolean assertMoneySql = assertSql( BeforeMoney,    AfterMoney,cases);
            System.out.println("数据库断言结果:"+assertMoneySql);
        }
    }

    public  boolean assertSql(Object BeforeMoney,   Object  AfterMoney,Cases  cases){
        //通过JSONPath获取amount字段值并转成String类型
        //{"member_id":"8051016","amount":"100"}
        String   moneyStr=JSONPath.read(cases.getParams(),"$.amount" ).toString();
        //BigDecimal类型不能直接转成long类型,可以转成String,再转成Double类型
        String BeforeMoneyStr = BeforeMoney.toString();
        String AfterMoneyStr = AfterMoney.toString();
        double BeforeMoneyDouble = Double.parseDouble( BeforeMoneyStr);
        double AfterMoneyDouble = Double.parseDouble( AfterMoneyStr);
        double moneyDouble = Double.parseDouble( moneyStr);
        //返回差额
        return AfterMoneyDouble-BeforeMoneyDouble==moneyDouble;
        
        
    }
    
    
    
    // 数据支持,与test()方法发生关联,提供url和params
    @DataProvider(name = "datas")
    public Object[][] datas() {
        Object[][] datas = DataUtils.getApiandCasesByApiId("3");
        return datas;
    }

}



image.png
上一篇 下一篇

猜你喜欢

热点阅读