如何反射性地比较各个领域的POJO

Gau*_*rma 11 java junit testng unit-testing mockito

我基本上是在寻找一个单元测试框架,我可以用它来比较不覆盖equals和hascode方法的POJO.我看了一下JUnit,Test NG和Mockito,但他们似乎没有解决目的.

例如,考虑以下代码:

public class CarBean { 

    private String brand;
    private String color;

    public CarBean (){
    }

    public CarBean (String brand, String color){
        this.brand= brand;
        this.color= color;
    }

    /**
     * @return the brand
     */
    public String getBrand() {
        return brand;
    }

    /**
     * @param the brand to set
     */
    public void setBrand(String brand) {
        this.brand= brand;
    }

    /**
     * @return the color
     */
    public String getColor() {
        return color;
    }

    /**
     * @param the color to set
     */
    public void setColor(String color) {
        this.color= color;
    }
}
Run Code Online (Sandbox Code Playgroud)

POJO CarBean代表了一辆真实世界的汽车.它有两个参数,品牌和颜色.现在,假设您有两个汽车对象,如下所示:

CarBean car1 = new CarBean("Ford","Black");
CarBean car2 = new CarBean("Ford","Black");
Run Code Online (Sandbox Code Playgroud)

两个对象都具有相同的参数值.但是当你使用equals比较它时,它返回false:

car1.equals(car2); // This returns false
Run Code Online (Sandbox Code Playgroud)

现在我需要对返回CarBean对象的方法进行单元测试.在这种情况下,我要么需要逐个比较carbean属性,要么我需要实现equals()和hashcode()方法.

所以我的问题是 - 是否已经有一个单元测试框架可以处理这个?

dee*_*mar 6

覆盖 pojo 类中的 toString() 方法,如下所示

@Override
public String toString() {
    return "brand: " + this.brand + ",color: " + this.color;
}


car1.toString().equals(car2.toString()); //It will return true if both objects has same values
Run Code Online (Sandbox Code Playgroud)

如果您有大量参数,我建议您使用以下代码

public static boolean comparePOJO(Object obj1, Object obj2) {
    return new Gson().toJson(obj1).equals(new Gson().toJson(obj2));
}
comparePOJO(car1,car2); //It will return true
Run Code Online (Sandbox Code Playgroud)


Jef*_*ica 5

这种类型的反射作为SamePropertyValuesAs内置在 Hamcrest 中,它比较 bean 命名的属性(getFoo、isBar)而不是可能为它们提供动力的字段。核心 Hamcrest 支持内置于 JUnit 中,因此您只需添加包含 SamePropertyValuesAs 匹配器的 Hamcrest 库。

assertThat(car1, samePropertyValuesAs(car2));
Run Code Online (Sandbox Code Playgroud)


Gau*_*rma 5

Unitils 似乎确实为我解决了这个问题。以下 API 可以反射性地比较对象。即使 POJO 本身的属性是用户定义的 POJO,它也可以进行比较:

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 the 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 only the firstName property 
assertPropertyLenientEquals("firstName", Arrays.asList("John", "Jane"),
                                 new Person[] {new Person("Jane", "Doe"), new Person("John", "Doe")});
Run Code Online (Sandbox Code Playgroud)

更多信息参见 Unitils Cookbook