溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何在Java中使用rest assured對(duì)接口進(jìn)行測(cè)試

發(fā)布時(shí)間:2021-03-09 16:48:50 來(lái)源:億速云 閱讀:147 作者:Leah 欄目:編程語(yǔ)言

如何在Java中使用rest assured對(duì)接口進(jìn)行測(cè)試?針對(duì)這個(gè)問題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問題的小伙伴找到更簡(jiǎn)單易行的方法。

假設(shè)你寫了一個(gè)接口:lotto,訪問路徑是: http://localhost:8080/lotto

接口返回值:

{
"lotto":{
 "lottoId":5,
 "winning-numbers":[2,45,34,23,7,5,3],
 "winners":[{
  "winnerId":23,
  "numbers":[2,45,34,23,3,5]
 },{
  "winnerId":54,
  "numbers":[52,3,12,11,18,22]
 }]
}
}

如何快速的驗(yàn)證接口是否返回正常值呢?

get("/lotto").then().body("lotto.winners.winnerId", hasItems(23, 54));

使用簡(jiǎn)單吧!

引入

不多說,直接maven的方式引入:注意,我直接按照默認(rèn)的scope引入的,不是test;

主要引入以下2個(gè)依賴,原因如下:

rest-assured: 主要測(cè)試基本的http的rest風(fēng)格接口,這個(gè)是最基礎(chǔ)的依賴;

json-path: 主流的接口主要返回json,對(duì)接口進(jìn)行測(cè)試用例測(cè)試,主要也是判斷json返回某路徑下的數(shù)據(jù);

<dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>rest-assured</artifactId>
   <version>4.2.0</version>
</dependency>
<dependency>
   <groupId>io.rest-assured</groupId>
   <artifactId>json-path</artifactId>
   <version>4.2.0</version>
</dependency>

然后你就可以愉快的編寫測(cè)試用例,然后使用rest-assured進(jìn)行接口測(cè)試了。

使用要點(diǎn)

先上簡(jiǎn)單代碼吧!

先準(zhǔn)備測(cè)試數(shù)據(jù):

 final TestCaseDataModel<LoginRestReq> testCaseDataModel = new TestCaseDataModel<>();

    final LoginRestReq loginRestReq = LoginRestReq.builder()
        .appId("2a6bf452219cfe44c7f78231e3c80a13072b6727")
        .nonce("123456")
        .timestamp(System.currentTimeMillis())
        .userId("lxlifuchun")
        .userName("李福春")
        .build();
    String appSecret = "91e47f584dae551170ade272b2c7a69f";
    loginRestReq.setChecksum(SignUtils.generateCheckSum(loginRestReq.getAppId(), appSecret, loginRestReq.getTimestamp(), loginRestReq.getNonce()));

    testCaseDataModel.setInputParam(loginRestReq);


    ExpectModel expectModel = new ExpectModel();
    expectModel.setPath("data.id");
    expectModel.setMatcher(Matchers.lessThan(0));

    testCaseDataModel.setExpectResult(Arrays.asList(expectModel));
RestAssured.baseURI = "https://rest-beta.xxx.com";
  final ValidatableResponse validatableResponse = given().contentType(ContentType.JSON)
        .header("requestId", UUID.randomUUID().toString())
        .body(testCaseData.getInputParam()).
            post("/user_service/user/login")
        .then().contentType(ContentType.JSON);


  for (Object obj : testCaseData.getExpectResult()) {
      ExpectModel item = (ExpectModel) obj;
      validatableResponse.body(item.getPath(), item.getMatcher());
    }

做的事情很簡(jiǎn)單,就是拿一個(gè)登錄接口來(lái)實(shí)際的試一下:

login接口接受一個(gè)json的參數(shù),LoginRestReq對(duì)下轉(zhuǎn)換之后得到;
然后返回?cái)?shù)據(jù),數(shù)據(jù)中有一個(gè)用戶id,路徑是 data.id,如果id大于0,標(biāo)識(shí)登錄操作成功,登錄接口正常。

關(guān)于如何在Java中使用rest assured對(duì)接口進(jìn)行測(cè)試問題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI