-2

I have a simple java program that need to test that two lists of POJO elements are equal.

public void i_have_following(List<Post> myPost) throws Throwable {

        //retrieve list of element from rest end point
        String url = "http://jsonplaceholder.typicode.com/posts/";
        RestTemplate restTemplate = new RestTemplate();
        ParameterizedTypeReference<List<Post>> responseType = new ParameterizedTypeReference<List<Post>>() {
        };
        ResponseEntity<List<Post>> responseEntity = restTemplate.exchange(url,
                HttpMethod.GET, null, responseType);
        List<Post> allPosts = responseEntity.getBody();

        //get size=1 sublist of it and compare with expected local value
        List<Post> firstPost = allPosts.subList(0, 1);
        System.out.println("size of firstPost = " + firstPost.size());
        System.out.println("size of myPost = " + myPost.size());

        Assert.assertEquals(firstPost.size(), myPost.size());
        Assert.assertEquals(firstPost.get(0).getUserId(), myPost.get(0)
                .getUserId());
        Assert.assertEquals(firstPost.get(0).getId(), myPost.get(0).getId());
        Assert.assertEquals(firstPost.get(0).getTitle(), myPost.get(0)
                .getTitle());
        Assert.assertEquals(firstPost.get(0).getBody(), myPost.get(0).getBody());

        Assert.assertTrue(firstPost.equals(myPost));       //FAIL!!
        Assert.assertTrue(firstPost.containsAll(myPost));  //FAIL!!
    }

and the console output is:

size of firstPost = 1
size of myPost = 1

java.lang.AssertionError
    at org.junit.Assert.fail(Assert.java:86)
    at org.junit.Assert.assertTrue(Assert.java:41)
    at org.junit.Assert.assertTrue(Assert.java:52)

The element "POST" is just a simple POJO element:

public class Post {

    private Integer userId;
    private Integer id;
    private String title;
    private String body;

    /**
     * 
     * @return The userId
     */
    public Integer getUserId() {
        return userId;
    }

    /**
     * 
     * @param userId
     *            The userId
     */
    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    /**
     * 
     * @return The id
     */
    public Integer getId() {
        return id;
    }

    /**
     * 
     * @param id
     *            The id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * 
     * @return The title
     */
    public String getTitle() {
        return title;
    }

    /**
     * 
     * @param title
     *            The title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    /**
     * 
     * @return The body
     */
    public String getBody() {
        return body;
    }

    /**
     * 
     * @param body
     *            The body
     */
    public void setBody(String body) {
        this.body = body;
    }
}

From the print out and the assertions, it's obvious that these two list have the same size=1, and they have the same value of their only list element (all the assertions before the last two are TRUE).

I am really confused why the last two assertions can fail. I assume equals() and containsAll() are common api for list comparision and I am using them correctly.

Could anyone give me a hint what is missing here?

Appreciate it.

user1559625
  • 2,583
  • 5
  • 37
  • 75

2 Answers2

2

You compare 2 the same items, You should override method equals or use Java Comparable Interface: https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html

amkz
  • 568
  • 3
  • 9
  • 31
  • Thanks. Could you give a little more information? I thought a POJO has a default equal() based on its member variable's equal(). – user1559625 Feb 03 '16 at 11:36
  • See here : http://stackoverflow.com/questions/9277813/the-behaviour-of-equals-method-in-java or here: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#equals(java.lang.Object) – amkz Feb 03 '16 at 11:53
0

What about Assert.assertEquals(firstPost.get(0), mytPost.get(0) ? I bet you it fails... Your two objects are just not equal.

Dici
  • 25,226
  • 7
  • 41
  • 82