Hi I've been using these Json paths in the past but for some reason I've encountered an issue I can't seem to solve. I think it is something to do with my response having an array? Now when I tried yesterday I got a strange response which I'm no longer getting I'm now getting an error.
java.lang.ClassCastException: class java.util.ArrayList cannot be cast to class org.hamcrest.Matcher (java.util.ArrayList is in module java.base of loader 'bootstrap'; org.hamcrest.Matcher is in unnamed module of loader 'app')
package skySportsPages;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import org.testng.annotations.Test;
import java.io.File;
public class test extends enablers {
@Test
public void GetArticlesJson() {
JsonPath expectedJson = new JsonPath(new File("src/test/java/jsonFiles/articles.json"));
given().
when().
get(allArticles).
then().
assertThat().
contentType(ContentType.JSON).
and().
body("", hasItem(expectedJson.get("")));
}
}
POM file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>skysport</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.4.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured</artifactId>
<version>4.3.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>rest-assured-common</artifactId>
<version>4.3.3</version>
</dependency>
<dependency>
<groupId>io.rest-assured</groupId>
<artifactId>json-path</artifactId>
<version>4.3.3</version>
</dependency>
</dependencies>
</project>
Response I should get.
[
{
"id": "1",
"createdAt": "2020-10-27T19:10:01.232Z",
"title": "title 1",
"sensitive": false,
"topics": [
"business",
"politics",
"technology"
],
"image": null,
"priority": 89
},
{
"id": "2",
"createdAt": "2020-10-28T07:45:21.526Z",
"title": "title 2",
"sensitive": true,
"topics": [],
"image": {
"url": "https://imgur.com",
"fileType": ".jpg"
},
"priority": 45
},
{
"id": "3",
"createdAt": "2020-10-27T13:09:32.347Z",
"title": "£31 million pounds shortfall in economy",
"sensitive": false,
"topics": [],
"image": {
"url": "https://imgur.com",
"fileType": ".svg"
},
"priority": 25
},
{
"id": "4656364867443",
"createdAt": "2020-10-27T17:03:33.158Z",
"title": "title 4",
"sensitive": true,
"topics": [
"USA"
],
"image": {},
"priority": 0
},
{
"id": "5",
"createdAt": "2020-10-28T02:46:16.766Z",
"title": "title 5",
"sensitive": false,
"topics": [],
"image": {},
"priority": 56
}
]