Junit of equals method

I just want to know if there is a best practice or a common way of testing equal to the implementation in objects. I am referring to a validation method that has been overridden.

public boolean equals(Object o) 

I used that kind of logic. (Assume the number and name must be equal in order to get the truth)

 Dog d1 = new Dog(1,"Alvin"); Dog d2 = new Dog(2,"Alvin"); Assert.assertFalse(d1.equals(null)); Assert.assertFalse(d1.equals(d2)); d2.setId(1); d2.setName("Kelvin"); Assert.assertFalse(d1.equals(d2)); d2.setName("Alvin"); Assert.assertTrue(d1.equals(d2)); Assert.assertTrue(d1.equals(d1)); 

But when there are many fields, this task is very large and boring, so my question is whether there is any framework, tool, option, everything that makes this easier, and can prove that the method has been overridden correctly. thanks.

I know that overriding a method is equal to depending on the required logic, but also creating a test case, so you are looking for a standard way to test a method that avoids large codes in test cases if there is an explicit or some kind of sentence you may have.

+7
java equality equals junit testing
source share
2 answers

Having looked at the duplicate question in the link, I read the @ user598656 answer and suggest using the Tool (BABY) (Automated tester java bean)

Reading the documentation I found this feature.

5.2.7. Property! Value! Test! Algorithm! The algorithm for testing the significance of properties is as follows:

 for each property in public getter/setter method pairs do create instance of class under test, object x create instance of class under test, object y change property of y to contain a different value if property is insignificant then assert x.equals(y) else assert x.equals(y) is false end if end for 

This is what I was looking for, the last answer, but, in my opinion, it fits my needs.

+2
source share

You can use templates based on Fast code ecllipse plugin fields. You can write a simple template and select many fields that you want from the class to generate statements using these fields.

+2
source share

All Articles