Re: Newbie question: Expectations and object references

View: New views
1 Messages — Rating Filter:   Alert me  

Parent Message unknown Re: Newbie question: Expectations and object references

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Welcome,

Your diagnosis is right, the default comparison uses equals(), so if  
there's no implementation it will revert to object identity.

What is distinctive about an instance of TestClass that you want to  
confirm? For example, is there something about the key that is visible?

S.

On 1 Oct 2009, at 23:59, jim.s wrote:

> Hello,
>
> I'm new to JMock (using with TestNG) and I have a question about a the
> following scenario:
>
> I am testing a dao that takes a parameter and then creates the  
> object that
> will be persisted.
>
> public class TestService {
>
>    ITestDao testDao;
>
>    public void save(KeyClass key) {
>
>        TestClass testClass = new TestClass(key);
>        testDao.save(testClass);
>    }
> }
>
> Here is what my test looks like, at the moment.
>
> class TestService {
>
>   public void testClassCreate() {
>
>       final ITestDao testDao = context.mock(ITestDao.class);
>
>       TestService testService = new TestService();
>       testService.setTestDao(testDao);
>
>       final TestClass testClass = new TestClass(new  
> KeyClass("testkey"));
>
>       context.checking(new Expectations() {{
>           oneOf (registryDao).save(testClass);
>       }});
>
>       context.assertIsSatisfied();
>
>    }
> }
>
> When I run this I get something similar to the following:
>
> FAILED: testClassCreate
> not all expectations were satisfied
> expectations:
>  expected once, never invoked:
> iTestDao.save(<com.xxx.xxx.xxxx.domain.TestClass@ab3738ea>);
>
>
> So, I think that the issue is that the TestClass instance being  
> saved isn't
> the same object reference as the actual saved TestClass (due to object
> creation using the key parameter).
>
> Is there any way to handle this? I've messed around with  
> Expectations, but
> no luck. Do I need a custom matcher?
>
> Any help appreciated!
>
> Thanks,
> Jim

Steve Freeman
Winner of the Agile Alliance Gordon Pask award 2006

http://www.m3p.co.uk

M3P Limited.
Registered office. 2 Church Street, Burnham, Bucks, SL1 7HZ.
Company registered in England & Wales. Number 03689627



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email