views:

326

answers:

1

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 ???

+1  A: 

Hi Arthur

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

Thomas Rawyler
@Thomas Thans for your reply, Thomas. ok about boolean equals. Have you done as shown above ? I am using 2.5.2 version
Arthur Ronald F D Garcia
@Arthur I changed it to 2.5.2 and it still works, from command line and in eclipse. I really copy pasted all your code as it is written above and added getters and setters.
Thomas Rawyler
@Thomas You are right. I missed some code in equals implementation. Accepted. Good to know EasyMock supports Generic
Arthur Ronald F D Garcia
@Arthur Thanks. I recomend Mockito for better readability (http://mockito.org/). It's a fork of EasyMock and includes the EasyMock ClassExtensions in the default package.
Thomas Rawyler