-1

Checkout my screenshot:

screenshot depicting AssertionError

@Test
public void getAllEmployeesTest() throws IOException {
    HttpResponse response = http.get("http://localhost:8087/employee");
    List<Employee> expectedList = new ArrayList<Employee>();
    expectedList.add(new Employee(2, "Yashwant", "Chavan", 30, true));
    List<Employee> actualList = gson.fromJson(EntityUtils.toString(response.getEntity()), new ArrayList<Employee>().getClass());
    Assert.assertEquals(actualList, expectedList);
}
Jonathan
  • 20,053
  • 6
  • 63
  • 70
nhxkutex
  • 5
  • 2
  • 2
    Hi! I edited your question to display the image. It would be better if you included the text of the error in the question rather than using screenshots. – Jonathan May 30 '18 at 12:37

3 Answers3

1

If the Employee class does not implement the equals method, then Object equals method is used. Object equals method will return true iff both the objects are equal.

If you do not want to implement equals method, you can use the ReflectionAssert.assertReflectionEquals (https://mvnrepository.com/artifact/org.unitils/unitils-core/3.4.6) which compares the fields in the two objects via reflection.

pulkit-singhal
  • 845
  • 5
  • 15
0

You need to tell us more about what you expect to see and what you're actually seeing. However, I'll take a guess that Assert.assertEquals(actualList, expectedList); fails. I assume too that your employee endpoint is supposed to return that one matching employee...

So, does your Employee class implement equals(Object other) sensibly? If so, you should be able to do as you're doing. If not, make it so and try again.

See also Assert about a List in Junit

  • i get a jsonformat output in my actual, maybe thats why? – nhxkutex May 30 '18 at 12:32
  • Er, yes. The screenshot (which I can now see) makes that clear. –  May 30 '18 at 14:27
  • I asked the OP for more clarification. It's obvious to me that his actual and expected differ. I see plenty of comments along the lines of "SO is not a homework factory" so I am leaving this as it is. –  May 30 '18 at 14:55
0
  1. As others said, make sure your Employee class implements a reasonable equals(). If it's just a plain POJO, equals generated
  2. The correct use of assertEquals() is assertEquals(expected, actual). Use it that to have the equals() method of the expected object to be invoked (rather than the other way around, which may or may not be a problem).
  3. Refer to this question for a way to deserialize a List with Gson: Google Gson - deserialize list<class> object? (generic type)
david a.
  • 5,283
  • 22
  • 24