Omar Sabha
Omar Sabha

Reputation: 23

Getting assertionError using restassured on Json repsonse field value and the expected and actual are identical

Here is my code after getting the response.

response.then().assertThat().body("documents[0].items[0].price.sales_tax_summary[0].rate",equalTo("0.0"));

here is the response log:

{
    "id": "quote-1",
    "documents": [
        {
            "id": "document-1",
            "items": [
                {
                    "id": "2",
                    "price": {
                        "amount_inclusive": 200000.5,
                        "amount_exclusive": 200000.5,
                        "total_tax": 0.0,
                        "tax_rate": 0.0,
                        "sales_tax_summary": [
                            {
                                "rate": 0.0,
                                "amount": 0.0
                            },
                            {
                                "rate": 0.0,
                                "amount": 0.0
                            }
                        ]
                    }
                }
            ],
            "shipping": {
                "id": "3",
                "price": {
                    "amount_inclusive": 15.0,
                    "amount_exclusive": 15.0,
                    "total_tax": 0.0,
                    "tax_rate": 0.0,
                    "sales_tax_summary": [
                        {
                            "rate": 0.0,
                            "amount": 0.0
                        },
                        {
                            "rate": 0.0,
                            "amount": 0.0
                        }
                    ]
                }
            },
            "handling": {
                "id": "4",
                "price": {
                    "amount_inclusive": 5.0,
                    "amount_exclusive": 5.0,
                    "total_tax": 0.0,
                    "tax_rate": 0.0,
                    "sales_tax_summary": [
                        {
                            "rate": 0.0,
                            "amount": 0.0
                        },
                        {
                            "rate": 0.0,
                            "amount": 0.0
                        }
                    ]
                }
            }
        }
    ]
}

here is the error message

java.lang.AssertionError: 1 expectation failed.
JSON path documents[0].items[0].price.sales_tax_summary[0].rate doesn't match.
Expected: 0.0
  Actual: 0.0

I have tried trimming, using containing substring, plugging 0.0 as double values instead of string as expected. but got the same error

Upvotes: 2

Views: 1355

Answers (2)

Deepak Attri
Deepak Attri

Reputation: 132

Your JSON returns a double value and you are comparing it with a string, that's the reason your Assertion is failing. Use equalTo() when comparing two strings. Use the following code:

response.then().assertThat().body("documents[0].items[0].price.sales_tax_summary[0].rate"== 0.0)

Upvotes: 0

Dave Gorecki
Dave Gorecki

Reputation: 26

Comparing a String to a double is the issue.

Try this instead:

double expectedRate = 0.0;

double actualRate = response.then().extract().jsonPath().getDouble("documents[0].items[0].price.sales_tax_summary[0].rate");

assertTrue(expectedRate == actualRate);

That will make sure it compares as doubles instead of a string to a double and should get you the result you're looking for.

Upvotes: 1

Related Questions