Hi,
Sorry for inconvenience. I have a question on jMock use. Would you PLS give me some comments?
I have a class Foo and an interface Goo like below:
public class Foo {
private Goo goo;
private int i;
public Foo (Goo aGoo) {
goo = aGoo;
i = goo.getCount();
}
public int getI (){
return i;
}
public void incre () {
goo.incre();
i = goo.getCount();
}
}
public interface Goo {
public void incre ();
public int getCount ();
}
And I set up a test case like below:
public class oneTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testSth () {
Mockery mockery = new Mockery();
final Goo mockGoo = mockery.mock(Goo.class, "MockGoo");
mockery.checking(new Expectations() {
{
one(mockGoo).incre();
one(mockGoo).getCount();
will(returnValue(1));
}
});
Foo aFoo = new Foo(mockGoo);
aFoo.incre();
mockery.assertIsSatisfied();
}
}
But after I run the test case I get below error. And if I remove “i = goo.getCount();” from Foo’s constructor, it is OK. Does it means that we sho
uld not use the mock objects (in my code is Goo)’ method in the constructor of the class (in my code is Foo) to be tested?
Thank you very much!
- Wei Yang
Error:
unexpected invocation: MockGoo.getCount()expectations: expected once, already invoked 1 time: MockGoo.incre(); returns a default value expected once, already invoked 1 time: MockGoo.getCount(); returns <1>what happened before this: MockGoo.getCount() MockGoo.incre() at org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56) at
org.jmock.Mockery.dispatch(Mockery.java:218) at
org.jmock.Mockery.access$000(Mockery.java:43) at
org.jmock.Mockery$MockObject.invoke(Mockery.java:258) at
org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27) at
org.jmock.internal.FakeObjectMethods.invoke(FakeObjectMethods.java:38) at org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33) at
$Proxy45.getCount(Unknown Source) at myTest.Foo.incre(Foo.java:18) at
myTest.oneTest.testSth(oneTest.java:32) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)