RestAssured comparing Double with Hamcrest

≯℡__Kan透↙ 提交于 2019-12-11 07:28:50

问题


I have a stupid thing, but I really can´t see what am I missing:

I have a test:

@Test
public void testeBerechneRendite() {
    get("/rendite?jahresnettomiete=8000&kaufpreis=100000&nebenkosten=500")
    .then().body(is(closeTo(0.079, 0.01)));
}

Error is:

Response body doesn't match expectation.
Expected: is a numeric value within <0.01> of <0.079>
Actual: 0.07960199004975124

It seams I don`t understand the closeTo(double, double). From my understanding all numbers between 0.069 and 0.089 should be valid. If I am totally wrong please clarify :-)


回答1:


Actual: 0.07960199004975124 - it is a string value, that is why your matcher does not work. You need to extract value, convert into double and compare in separate mathcer.

MatcherAssert.assertThat(
    Double.parseDouble(get("/rendite?jahresnettomiete=8000&kaufpreis=100000&nebenkosten=500")
    .then().extract().jsonPath().getString("args.val")),
    closeTo(0.079, 0.01)
);


来源:https://stackoverflow.com/questions/42121322/restassured-comparing-double-with-hamcrest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!