Search code examples
javaassertj

Java code works when referenced from a variable but throws an error when called directly


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

Solution

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