tags:

views:

36

answers:

3

Hi,

I'm trying to test an algorithm with Easymock but I'm stumbling into the details of the implementation of this algorithm. Someone who can provide me a way out? The part which gives me a problem is this:

interface A {
 B getB ();
}

interface B {
  void setX (int x);
  void doSomething ();
}

Now somewhere during the algorithm under test this happens:

a.getB ().setX (9);
a.getB ().doSomething ();
a.getB ().setX (16);

This results in an unexpected method call getB () as my test only declares the interesting part:

B b = EasyMock.createStrictControl ();
b.setX (9);
EasyMock.expectLastCall();
b.doSomething ();
EasyMock.expectLastCall();

I understand this is because the order is checked. But even when I place the following line nothing changes.

EasyMock.expect (a.getB ()).andReturn (b).anyTimes ();

The instance a is also an EasyMock proxy.

Anyone who can help me out?

+1  A: 

Use anyTimes() as per your last bit of code, but don't use strict mocks - it's the strictness which is enforcing the ordering.

Admittedly I can't remember the details of how EasyMock handles ordering between controls, but it sounds like you're really not bothered about the ordering.

Jon Skeet
I am bothered by the order of the methods of interface B but not about the order of where I call getB () on A.
A: 

Apparently andStubReturn (b) does the trick.

A: 

The following works for me fine. I am using strict mocks but I don't have any experience with the createStrictControl() method.

B b = EasyMock.createStrictMock(B.class);
A a = EasyMock.createStrictMock(A.class);
expect(a.getB()).andReturn(b).anyTimes();

b.setX(9);
b.doSomething();
b.setX(16);

replay(a);
replay(b);
a.getB().setX(9);
a.getB().doSomething();
a.getB().setX(16);
verify(b);
verify(a);
Gray