What should i do to test EasyMock objects when using Generics ? EasyMock
See code just bellow
Our generic interface
public interface Repository<INSTANCE_CLASS, INSTANCE_ID_CLASS> {
void add(INSTANCE_CLASS instance);
INSTANCE_CLASS getById(INSTANCE_ID_CLASS id);
}
And a single class
public class Order {
private Integer id;
private Integer orderNumber;
// getter's and setter's
public void equals(Object o) {
if(o == null)
return false;
if(!(o instanceof Order))
return false;
// business key
if(getOrderNumber() == null)
return false;
final Order other = (Order) o;
if(!(getOrderNumber().equals(other.getOrderNumber())))
return false;
return true;
}
// hashcode
}
And when i do the following test
private Repository<Order, Integer> repository;
@Before
public void setUp {
repository = EasyMock.createMock(Repository.class);
Order order = new Order();
order.setOrderNumber(new Integer(1));
repository.add(order);
EasyMock.expectLasCall().once();
EasyMock.replay(repository);
}
@Test
public void addOrder() {
Order order = new Order();
order.setOrderNumber(new Integer(1));
repository.add(order);
EasyMock.verify(repository)
}
I get
Unexpected method call add(br.com.smac.model.domain.Order@ac66b62):
add(br.com.smac.model.domain.Order@ac66b62): expected: 1, actual: 0
开发者_运维问答 at org.easymock.internal.MockInvocationHandler.invoke(MockInvocationHandler.java:43)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:72)
at $Proxy4.add(Unknown Source)
Why does it not work as expected ??? What should i do to pass the test ??? Any workaround ???
I copied your code and ran it with Java 1.6 using EasyMock 2.0 and it works just fine. I had to fix some parts like public void equals(Object o)
which should be public boolean equals
, but it seems clear that you had it right in your real code.
Are you using another EasyMock version or do I miss something?
Cheers Thomas
精彩评论