Search code examples
testingcomparedoublerest-assuredhamcrest

RestAssured comparing Double with Hamcrest


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 :-)


Solution

  • 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)
    );