java - How can I compare POJOs by their fields reflectively -


i looking unit testing framework, can use compare pojos don't override equals , hascode methods. had @ junit, test ng , mockito don't seem solve purpose.

for example consider code below :

public class carbean {       private string brand;     private string color;      public carbean (){     }      public carbean (string brand, string color){         this.brand= brand;         this.color= color;     }      /**      * @return brand      */     public string getbrand() {         return brand;     }      /**      * @param brand set      */     public void setbrand(string brand) {         this.brand= brand;     }      /**      * @return color      */     public string getcolor() {         return color;     }      /**      * @param color set      */     public void setcolor(string color) {         this.color= color;     } } 

the pojo carbean represents real world car. has 2 parameters, brand , color. now, suppose have 2 car objects below :

carbean car1 = new carbean("ford","black"); carbean car2 = new carbean("ford","black"); 

both objects have same parameter values. when compare using equals, returns false :

car1.equals(car2); // returns false 

now need unit test method returns carbean object. in scenario either need compare carbean attributes 1 one or need implement equals() , hashcode() methods.

so question - there unit testing framework can handle ?

unitils seems solve purpose me. following apis can compare objects reflectively. can compare if attributes of pojo user defined pojos :

import static org.unitils.reflectionassert.reflectionassert.*;  // exact field-by-field comparison assertreflectionequals(new person("john", "doe", new address("new street", 5, "brussels")),                                   new person("john", "doe", new address("new street", 5, "brussels"));  // ignore null / 0 values in expected object assertreflectionequals(new person("john", null, new address("new street", 0, null)),                                  new person("john", "doe", new address("new street", 5, "brussels"),                                   reflectioncomparatormode.ignore_defaults);   // ignore collection order assertreflectionequals(arrays.aslist(new person("john"), new person("jane")),                                  new person[] {new person("jane"), new person("john")},                                   reflectioncomparatormode.lenient_order);  // ignore null/0 values + collection order assertlenientequals(arrays.aslist(new person("john"), null),                                  new person[] {new person("jane", "doe"), new person("john", "doe")});  // check firstname property  assertpropertylenientequals("firstname", arrays.aslist("john", "jane"),                                  new person[] {new person("jane", "doe"), new person("john", "doe")}); 

further information @ unitils cookbook


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -