Java Mocktio 在 eq(object) 处验证失败,即使对象看起来相同

Java Mocktio verify failed at eq(object) even though the objects look the same

这是伪代码,

myFunc(class1, class2)

单元测试中

 verify(myService).myFunc(eq(obj1), eq(obj2));

但是得到了这些输出:

参数不同!通缉: ...

实际调用有不同的参数: ...

我将 obj1 和 obj2 的输出复制到文件 1 和文件 2,“diff file 1 file2”显示没有区别。 有什么建议吗?

很可能,obj1 和 obj2 的类型没有适当的 equalshashCode 函数。 equals 函数的职责是检查两个对象的内容是否相同。示例:

class MyClass {
    public int someValue = 17;

    public boolean equals(Object o) {
        if (o != null && o.getClass() == this.getClass()) {
            MyClass other = (MyClass) o;
            return o.someValue == this.someValue;
        } else {
            return false;
        }
    }
}