EasyMock problem, calling method on instance but not interested in how to get this instance
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?
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.
Apparently andStubReturn (b)
does the trick.
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);
精彩评论