PlanetJava
Custom Search

java-jmock-user
[Top] [All Lists]

RE: a question on jMock use

Subject: RE: a question on jMock use
Date: Wed, 20 Aug 2008 20:56:46 +0800
Do you mean that I should add something like "allowing(mockGoo).setFoo(aFoo)" 
into Expectations? If so, I should make the aFoo to final to use it in 
Expectations. It is not good.
I'd apprciate if you can give me some code snippet for my test case (Foo and 
Goo). I can not find similar cases from the Getting Started and Cookbook. It 
will be also very appreciated if you can lead me to cases similar with mine to 
solve my problem.
Thank you very much!
- Wei Yang
Date: Wed, 20 Aug 2008 12:18:59 +0200
From: jan.kodet-IzeFyvvaP7pWk0Htik3J/[email protected]
To: user-sXN/[email protected]
Subject: RE: [jmock-user] a question on jMock use
In 
your expectations, you state that method incre() on mockGoo should be 
called exactly one time and that method getCount() on mockGoo can be called 
any number of times (or not at all), each time returning 1. That is what you 
expect, nothing else.
Running the test, JMock noticed that a call to setFoo() was made on 
mockGoo, something that wasn't expected (since it wasn't in the expectations). 
The method invocation was not expected, thus the "unexpected invocation" 
:).
I 
found the Getting Started (http://jmock.org/getting-started.html) 
and the Cookbook (http://jmock.org/cookbook.html) a 
great way to get started with JMock. Also browsing through the mailing list 
(http://www.nabble.com/jMock-f2663.html) 
often gives valuable information.
Good 
luck!
/JMK
From: 杨威 [mailto:[email protected]] 
Sent: Wednesday, August 20, 2008 10:10 AM
To: 
user-sXN/[email protected]
Subject: RE: [jmock-user] a question on jMock 
use
Thank 
you very much! After I change to use "allowing" and "exactly(n)", it works 
well.
And I have another test case, I change Foo, Goo 
and  oneTest like below:
public 
class 
Foo {
    private 
Goo goo;
    private 
int 
i;
    public 
Foo (Goo aGoo) {
goo 
= aGoo;
i 
= goo.getCount();
goo.setFoo(this); 
//adding this line
    } 
    public 
int 
getI (){
return 
i;
    }
    public 
void 
incre () {
goo.incre();
i 
= goo.getCount();
    }
}
public 
interface 
Goo {
    public 
void 
incre ();
    public 
int 
getCount ();
    public 
void 
setFoo(Foo foo); //adding this line
}
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();
allowing(mockGoo).getCount();
will(returnValue(1));
}
});
    Foo aFoo = new 
Foo(mockGoo);
    aFoo.incre();
    mockery.assertIsSatisfied();
    }
}
After I run the 
test case, I also get "unexpected invocation" like below. Would 
you PLS help me again? 
Thanks!
unexpected 
invocation: 
MockGoo.setFoo(<[email protected]>)expectations: 
expected once, never invoked: MockGoo.incre(); returns a default value allowed, 
already invoked 1 time: MockGoo.getCount(); returns <1>what happened 
before this: MockGoo.getCount() 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.setFoo(Unknown Source) at 
com.vitria.bpmn.executor.Foo.<init>(Foo.java:10) at 
com.vitria.bpmn.executor.oneTest.testSth(oneTest.java:30) at sun.reflec 
t.NativeMethodAccessorImpl.invoke0(Native Method) at 
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at 
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 - Wei Yang
Date: Wed, 20 Aug 2008 09:42:21 +0200
From: jan.kodet-IzeFyvvaP7pWk0Htik3J/[email protected]
To: 
user-sXN/[email protected]
Subject: RE: [jmock-user] a question on jMock 
use
The 
expectation "one(mockGoo).getCount();" indicates that 
the getCount() method of mockGoo should be called exactly one time. As far as I 
see, it it called twice: the first time in the constructor and the other time 
in 
method incre(). So the expectation fails.
Depending on what it is you want to express, you can 
use some other of the cool stuff available in JMock, perhaps "exactly(n)" or 
"allowing". Check e.g. http://jmock.org/cardinality.html for more 
examples.
Cheers,
/JMK
From: 杨威 [mailto:[email protected]] 
Sent: Wednesday, August 20, 2008 9:30 AM
To: 
user-sXN/[email protected]
Subject: [jmock-user] a question on jMock 
use
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 
(){< /SPA N>
    return 
i;
    }
    public 
void incre () 
{
    goo.incre();
       i = 
goo.getCount();< /P&g 
t; 
    }
}
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 should 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)
MSN上小游戏,工作休闲两不误! 马上就开始玩! 
使用新一代 Windows Live Messenger 轻松交流和共享! 立刻下载! 
_________________________________________________________________
看MSN史诗巨片,票选人气角色,赢取PSP等诸多好礼!
http://im.msn.cn/
msgmiddle
<Prev in Thread] Current Thread [Next in Thread>
  • a question on jMock use
    • RE: a question on jMock use
      • RE: a question on jMock use
        • RE: a question on jMock use
        • RE: a question on jMock use,
        • RE: a question on jMock use
        • Re: a question on jMock use
        • Re: a question on jMock use
Current Sitemap | © 2012 planetjava | Contact | Privacy Policy