Validation of BigDecimal array?

314 views
Skip to first unread message

Mattias Severson

unread,
Oct 20, 2014, 7:44:17 AM10/20/14
to rest-a...@googlegroups.com
How can I make the following test pass?

@Before
public void setUp() {
        RestAssured.config = RestAssured.config().jsonConfig(jsonConfig().numberReturnType(BIG_DECIMAL));
}

@Test
public void returnsBigDecimalArray() {
        when().
                get("/example").
        then().
                statusCode(HttpStatus.SC_OK).
                content(arrayContaining(BigDecimal.valuOf(100), BigDecimal.valueOf(50), BigDecimal.valueOf(31.0)));
}


When executed, the test fails:
java.lang.AssertionError: Response body doesn't match expectation.
Expected: [<100>, <50>, <31.0>]
  Actual: [100,50,31.0]

When I print the response body (using log().all()) I get the following result:
[
    100,
    50,
    31.0
]


Johan Haleby

unread,
Oct 20, 2014, 7:57:32 AM10/20/14
to rest-a...@googlegroups.com
I've never used the "arrayContaining" matcher and when I read the javadoc it seems like it only works for arrays. RestAssured uses lists, so try the "hasItems" matcher instead and see if that works.

/Johan

--
You received this message because you are subscribed to the Google Groups "REST assured" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rest-assured...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Mattias Severson

unread,
Oct 20, 2014, 8:07:01 AM10/20/14
to rest-a...@googlegroups.com
I tried that initially, but it resulted in the following error:

java.lang.AssertionError: Response body doesn't match expectation.
Expected: (a collection containing <100> and a collection containing <50> and a collection containing <31.0>)
  Actual: [100,50,31.0]


/Mattias

Johan Haleby

unread,
Oct 20, 2014, 8:19:56 AM10/20/14
to rest-a...@googlegroups.com
If you get those kind of error messages it could be a good idea to try the Hamcrest default error messages instead (see errorDescriptionType in com.jayway.restassured.config.MatcherConfig).

Anyway I looked at your code a little more closely and know I see what's going on. There are several problems with the test but it's a bit tricky :). First off all you're using the hamcrest matcher to validate the entire response and not a specific path. For example this matcher would word against the entire response body and not just a json/xml path:

.. .content(containsString("html")) .. 

This means that  "content(arrayContaining(BigDecimal.valueOf(100), BigDecimal.valueOf(50), BigDecimal.valueOf(31.0)))" will never work in your case since you're comparing the entire content with the "arrayContaining" matcher. What you want to do is to compare the unnamed root element with the "hasItems" matcher. An unnamed root element is indicated by an empty string as path or a $.

Secondly integers are not represented as BigDecimal's since integers are exact by definition. Only floats and doubles are represented as BigDecimal (see javadoc and usage guide). So your code should probably look like this:

when().
        get("/example").
then().
        statusCode(HttpStatus.SC_OK).
        content("$", hasItems(100, 50, BigDecimal.valueOf(31.0)));

/Johan


Mattias Severson

unread,
Oct 20, 2014, 9:00:14 AM10/20/14
to rest-a...@googlegroups.com
Right, I see. Thanks!

/Mattias
Reply all
Reply to author
Forward
0 new messages