如何将我的回复与新员工进行比较?

时间:2018-05-30 12:20:46

标签: java json rest http testng

查看我的屏幕截图:

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);
}

3 个答案:

答案 0 :(得分:1)

如果Employee类未实现equals方法,则使用Object等于方法。如果两个对象相等,则对象equals方法将返回true。

如果您不想实施equals方法,可以使用ReflectionAssert.assertReflectionEqualshttps://mvnrepository.com/artifact/org.unitils/unitils-core/3.4.6)通过反射比较两个对象中的字段。

答案 1 :(得分:0)

您需要告诉我们您希望看到的内容以及您实际看到的内容。但是,我会猜测Assert.assertEquals(actualList, expectedList);失败了。我也假设您的员工端点应该返回一个匹配的员工...

那么,您的Employee类是否明智地实现了equals(Object other)?如果是这样,你应该能够像你一样做。如果没有,请这样做,然后再试一次。

另见Assert about a List in Junit

答案 2 :(得分:0)

  1. 正如其他人所说,请确保您的Employee类实现合理的equals()。如果它只是一个普通的POJO,equals生成了
  2. 正确使用assertEquals()assertEquals(expected, actual)。使用它来调用预期对象的equals()方法(而不是相反,这可能是也可能不是问题)。
  3. 请参阅此问题,了解使用Gson反序列化List的方法:Google Gson - deserialize list<class> object? (generic type)
相关问题