简体   繁体   中英

Why am I getting a different expected response to my actual?

Im trying to match an actual response to expected Json file I have within my project which currently looks like this.

         [
          {
            "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": {},
        "p

riority": 56
  }
]

However when I get my expected and actual they look totally different to what I want? It seems to be placing a <> in my expected which isn't there.

Expected: a collection containing <[{createdAt=2020-10-27T19:10:01.232Z, image=null, topics=[busine

ss, politics, technology], id=1, sensitive=false, title=title 1, priority=89}, {createdAt=2020-10-28T07:45:21.526Z, image={url=https://imgur.com, fileType=.jpg}, topics=[], id=2, sensitive=true, title=title 2, priority=45}, {createdAt=2020-10-27T13:09:32.347Z, image={url=https://imgur.com, fileType=.svg}, topics=[], id=3, sensitive=false, title=£31 million pounds shortfall in economy, priority=25}, {createdAt=2020-10-27T17:03:33.158Z, image={}, topics=[USA], id=4656364867443, sensitive=true, title=title 4, priority=0}, {createdAt=2020-10-28T02:46:16.766Z, image={}, topics=[], id=5, sensitive=false, title=title 5, priority=56}]>
  
Actual: [{createdAt=2020-10-27T19:10:01.232Z, image=null, topics=[business, politics, technology], id=1, sensitive=false, title=title 1, priority=89}, {createdAt=2020-10-28T07:45:21.526Z, image={url=https://imgur.com, fileType=.jpg}, topics=[], id=2, sensitive=true, title=title 2, priority=45}, {createdAt=2020-10-27T13:09:32.347Z, image={url=https://imgur.com, fileType=.svg}, topics=[], id=3, sensitive=false, title=£31 million pounds shortfall in economy, priority=25}, {createdAt=2020-10-27T17:03:33.158Z, image={}, topics=[USA], id=4656364867443, sensitive=true, title=title 4, priority=0}, {createdAt=2020-10-28T02:46:16.766Z, image={}, topics=[], id=5, sensitive=false, title=title 5, priority=56}]

My code: I've played around with the expectedJson.getList thinking it might be something to do with that however I'm a bit stumped.

@Test
public void GetArticlesJson() {
    JsonPath expectedJson = new JsonPath(new File(pathJson));

    given().
      

      when().
                get(allArticles).
                then().
                assertThat().
                contentType(ContentType.JSON).
                and().
                body("", hasItem(expectedJson.getList("")));
        System.out.println("Expected " + expectedJson.getList(""));
    }

I think you can converse.toString() and compare them. Even though, you can put it in object and compare them or compare a small parameter like id,createdAt,etc... compare text evidence

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM