Search code examples
javaunit-testingassertjavabeansequality

Assert that two java beans are equivalent


This question is close, but still not what I want. I'd like to assert in a generic way that two bean objects are equivalent. In case they are not, I'd like a detailed error message explaining the difference instead of a boolean "equal" or "not equal".


Solution

  • I recommend you use unitils library:

    http://www.unitils.org/tutorial-reflectionassert.html

    public class User {
    
        private long id;
        private String first;
        private String last;
    
        public User(long id, String first, String last) {
            this.id = id;
            this.first = first;
            this.last = last;
        }
    }
    
    User user1 = new User(1, "John", "Doe");
    User user2 = new User(1, "John", "Doe");
    assertReflectionEquals(user1, user2);
    

    See also: