I have a debate with a colleague about the specific situation that I am facing, and it would be great if someone could hear some idea or justification of the theory.
Say we have model objects of type A. They are java beans, property owners, and have methods such as getPrice, getQuantity, getName ..
Suppose also that for some inherited reason, the equals method returns true on two different objects, even if they have different property values!
I will provide some code that illustrates this problem. (Obviously this is not the same code that was made using shortcuts)
class A {
private final double q;
private final double p;
public A(double q, double p) {
this.q = q;
this.p = p;
}
public double getQuantity()
{
return q;
}
public double getPrice()
{
return p;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
return true;
}
}
public abstract class Handler {
protected Manager m;
public Handler(Manager m) {
this.m = m;
}
abstract public void handle(A a);
}
class HandlerA extends Handler {
public HandlerA(Manager m) {
super(m);
}
@Override
public void handle(A a) {
m.f(a, "abc");
}
}
...
class HandlerC extends Handler {
public HandlerC(Manager m) {
super(m);
}
@Override
public void handle(A a) {
m.g(a, 1);
}
}
class Manager {
public void f(A a, String s) { }
public void g(A a, double q) { }
}
We want a unit test HandlerA.
So, we could write a test like this:
public class TestMain {
@Test
public void givenA_fHappens() {
Manager manager = mock(Manager.class);
HandlerA handler = new HandlerA(manager);
A givenA = new A(7, 9);
handler.handle(givenA);
verify(manager).f(givenA, "abc");
}
}
, equals true, A , :
@Override
public void handle(A a) {
-- m.f(a, "abc");
++ m.f(new A(1, 1), "abc");
}
unit test
, ( , )
, SamePropertyValueAs, ,
, , , .
? ?