0

I created an assertion using AssertJ library. I stored the extraction from a node contained in an API response in a string variable. I referenced the variable in the assertion. It works, but when the code is passed directly in the assertion, it throws a Predicate error.

//Style 1
JsonPath jsonPath = response.jsonPath();
String s = jsonPath.get("name"); //stored in a variable
Assertions.assertThat(s).contains("test"); // no error when referenced here

//Style 2
 JsonPath jsonPath = response.jsonPath();
 Assertions.assertThat(jsonPath.get("name")).contains("test"); //throws an error when used directly


//error
Ambiguous method call. Both
assertThat
(Predicate<Object>)
in Assertions and
assertThat
(IntPredicate)
in Assertions match
The man
  • 129
  • 16
  • If you are regularly doing JSON assertions, I would reocmmend https://github.com/lukas-krecan/JsonUnit which has support for JsonPath – Joel Costigliola Jul 26 '20 at 21:37

2 Answers2

1

Assertions.assertThat((String)jsonPath.get("name")) Cast to string it will work

1

It is something called Ambiguous method, basically due to the fact that T could implement an interface I and there is an assertThat(I) in Assertions making the compiler confused whether to use assertThat(String) or assertThat(I). So in your case, cast to string for the method: (String) jsonPath.get("name") should work

White Link
  • 106
  • 7