1. 程式人生 > 其它 >rest-auured斷言報錯Float與BigDecimal

rest-auured斷言報錯Float與BigDecimal

本章講解使用rest-auured斷言出現型別不匹配Float與BigDecimal的解決方法

一般使用金額時都會精確到小數,在excel或者其他檔案中自定義的響應資訊時,json解析小數會自動設定為Float型別,而介面響應的小數型別是BigDecimal型別,

所以在斷言時會出現型別轉換異常,以下有幾種解決方法:

rest-auured文件官網:https://github.com/rest-assured/rest-assured/wiki/Usage

1、在斷言處對Float進行轉換成BigDecimal型別在比較

        Float f = 5.02F;
        // 將Float型別轉為BigDecimal
BigDecimal bigDecimal = new BigDecimal(f.toString()); System.out.println(bigDecimal.getClass()); System.out.println(bigDecimal);

2、第二種方法rest-auured官網也做了解決方案:需要在give()方法後面加上一個配置:config(RestAssured.config().jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL)))

但是這種方法不推薦使用,因為每一個介面都要加這個,非常的麻煩,rest-assured提供了配置,只需要在@Test註解標註方法執行之前執行這個配置就可以

配置如下:RestAssured.config = RestAssured.config().jsonConfig(jsonConfig().numberReturnType(BIG_INTEGER));

注意:導包別導錯了,

import static io.restassured.config.JsonConfig.jsonConfig;
import static io.restassured.path.json.config.JsonPathConfig.NumberReturnType.BIG_INTEGER;