user3552178
user3552178

Reputation: 2971

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

This is the pseudo code,

myFunc(class1, class2)

in unit test

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

But got these outputs:

Argument(s) are different! Wanted: ...

Actual invocation has different arguments: ...

I copied the output of obj1 and obj2 to file 1 and file2, "diff file 1 file2" shows no difference. Any suggestions ?

Upvotes: 0

Views: 1193

Answers (1)

user7655213
user7655213

Reputation:

Most likely, the type of obj1 and obj2 does not have a proper equals and hashCode function. The responsibility of an equals function is to, well, check if two objects are identical in their content. Example:

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;
        }
    }
}

Upvotes: 1

Related Questions