Strange issue with aNonNull expectation

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

Strange issue with aNonNull expectation

by Thomas Becker-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Usergroup,

I'm using jmock more and more extensively in my Unit Test and I really
like it. Saves myself a bunch of work. However I got a strange
AssertionError which I do neither really understand nor do I get it
resolved.  Maybe you can shed some light on me.

Here's the test method:

    /**
     * Test method for {@link
net.netm.me.moritz.service.impl.TagServiceImpl#addTag(java.lang.String,
java.lang.String, int, Long)}.
     */
    @Test
    public void testAddGenreTag() throws TagAlreadyExistsException{
       
        // expectations
        context.checking(new Expectations() {{
             one (contractDao).findContract(contractId);
             will(returnValue(contract));
            one (tagDao).findTagByKeyForClientId(tagKey, clientId);
             one (tagTypeService).getTagType(genreTagType.getId());
             will (returnValue(genreTagType));
             one (tagDao).save(aNonNull(Tag.class));
        }});
       
        // method to test
        Tag newTag = tagService.addTag(tagKey, tagDescription,
genreTagType.getId(), contractId);
        assertTrue("new Genre Tag should start  with Genre",
newTag.getKey().startsWith("GENRE."));
    }

This is the expectation which fails:
one (tagDao).save(aNonNull(Tag.class));

And the FailureTrace:

java.lang.AssertionError: unexpected invocation:
tagDao.save(<Tag[id=0,key=GENRE.Take That,client.id=186,description=Tag
for Artist Take That,tagType.key=GENRE]>)
expectations:
  expected exactly 1 time, already invoked 1 time:
contractDao.findContract(<21009747L>); returns
<net.netm.me.core.model.Contract@15664f1a[id=21009747,name=<null>]>
  expected exactly 1 time, already invoked 1 time:
tagDao.findTagByKeyForClientId("Take That", <186L>); returns a default value
  expected exactly 1 time, already invoked 1 time:
tagTypeService.getTagType(<0>); returns <TagType[id=0,key=GENRE]>
  expected exactly 1 time, never invoked: tagDao.save(<not null>);
returns a default value
    at
org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
    at org.jmock.Mockery.dispatch(Mockery.java:204)
    at org.jmock.Mockery.access$000(Mockery.java:37)
    at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
    at
org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
    at
org.jmock.internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:36)
    at
org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
    at $Proxy7.save(Unknown Source)
    at
net.netm.me.moritz.service.impl.TagServiceImpl.createAndPersistTag(TagServiceImpl.java:257)
    at
net.netm.me.moritz.service.impl.TagServiceImpl.addTag(TagServiceImpl.java:128)
    at
net.netm.me.moritz.service.TagServiceTest.testAddGenreTag(TagServiceTest.java:262)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
    at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:36)
    at
org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
    at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
    at
org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
    at
org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
    at
org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
    at
org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
    at
org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
    at
org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
    at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

I excpect any Tag.class and tagDao.save gets called with a Tag.class  
parameter. So the test shouldn't fail at all?! But it does....any clue
why? Am I getting the aNonNull functionality wrong? It's the first time
I'm using it.

Thanks a lot for any help.

Cheers,
Thomas


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

    http://xircles.codehaus.org/manage_email



Re: Strange issue with aNonNull expectation

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I'm confused too.

The only thing I notice is that you seem to have some defaults set up  
on the tagDao, since the find method doesn't have a will() clause. Is  
that just a typo or is there more setup going on? Also, is Tag an  
interface or class and are there any overloaded methods?

S.

On 8 May 2008, at 08:25, Thomas Becker wrote:

> Hi Usergroup,
>
> I'm using jmock more and more extensively in my Unit Test and I  
> really like it. Saves myself a bunch of work. However I got a  
> strange AssertionError which I do neither really understand nor do I  
> get it resolved.  Maybe you can shed some light on me.
>
> Here's the test method:
>
>   /**
>    * Test method for {@link  
> net
> .netm.me.moritz.service.impl.TagServiceImpl#addTag(java.lang.String,  
> java.lang.String, int, Long)}.
>    */
>   @Test
>   public void testAddGenreTag() throws TagAlreadyExistsException{
>             // expectations
>       context.checking(new Expectations() {{
>            one (contractDao).findContract(contractId);
>            will(returnValue(contract));
>           one (tagDao).findTagByKeyForClientId(tagKey, clientId);
>            one (tagTypeService).getTagType(genreTagType.getId());
>            will (returnValue(genreTagType));
>            one (tagDao).save(aNonNull(Tag.class));
>       }});
>             // method to test
>       Tag newTag = tagService.addTag(tagKey, tagDescription,  
> genreTagType.getId(), contractId);
>       assertTrue("new Genre Tag should start  with Genre",  
> newTag.getKey().startsWith("GENRE."));
>   }
>
> This is the expectation which fails:
> one (tagDao).save(aNonNull(Tag.class));
>
> And the FailureTrace:
>
> java.lang.AssertionError: unexpected invocation:  
> tagDao.save(<Tag[id=0,key=GENRE.Take  
> That,client.id=186,description=Tag for Artist Take  
> That,tagType.key=GENRE]>)
> expectations:
> expected exactly 1 time, already invoked 1 time:  
> contractDao.findContract(<21009747L>); returns  
> <net.netm.me.core.model.Contract@15664f1a[id=21009747,name=<null>]>
> expected exactly 1 time, already invoked 1 time:  
> tagDao.findTagByKeyForClientId("Take That", <186L>); returns a  
> default value
> expected exactly 1 time, already invoked 1 time:  
> tagTypeService.getTagType(<0>); returns <TagType[id=0,key=GENRE]>
> expected exactly 1 time, never invoked: tagDao.save(<not null>);  
> returns a default value
>   at  
> org
> .jmock
> .internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
>   at org.jmock.Mockery.dispatch(Mockery.java:204)
>   at org.jmock.Mockery.access$000(Mockery.java:37)
>   at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
>   at  
> org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:
> 27)
>   at  
> org
> .jmock
> .internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:36)
>   at org.jmock.lib.JavaReflectionImposteriser
> $1.invoke(JavaReflectionImposteriser.java:33)
>   at $Proxy7.save(Unknown Source)
>   at  
> net
> .netm
> .me
> .moritz
> .service.impl.TagServiceImpl.createAndPersistTag(TagServiceImpl.java:
> 257)
>   at  
> net
> .netm
> .me.moritz.service.impl.TagServiceImpl.addTag(TagServiceImpl.java:128)
>   at  
> net
> .netm
> .me
> .moritz.service.TagServiceTest.testAddGenreTag(TagServiceTest.java:
> 262)
>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>   at  
> sun
> .reflect
> .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>   at  
> sun
> .reflect
> .DelegatingMethodAccessorImpl
> .invoke(DelegatingMethodAccessorImpl.java:25)
>   at java.lang.reflect.Method.invoke(Method.java:597)
>   at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
>   at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:36)
>   at  
> org
> .junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:
> 98)
>   at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:
> 79)
>   at  
> org
> .junit
> .internal
> .runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:
> 87)
>   at  
> org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
>   at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
>   at  
> org
> .junit
> .internal
> .runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
>   at  
> org
> .junit
> .internal
> .runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
>   at org.junit.internal.runners.JUnit4ClassRunner
> $1.run(JUnit4ClassRunner.java:44)
>   at  
> org
> .junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:
> 27)
>   at  
> org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
> 37)
>   at  
> org
> .junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:
> 42)
>   at  
> org
> .eclipse
> .jdt
> .internal
> .junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
>   at  
> org
> .eclipse
> .jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>   at  
> org
> .eclipse
> .jdt
> .internal
> .junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>   at  
> org
> .eclipse
> .jdt
> .internal
> .junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>   at  
> org
> .eclipse
> .jdt
> .internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
>   at  
> org
> .eclipse
> .jdt
> .internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
> 196)
>
> I excpect any Tag.class and tagDao.save gets called with a  
> Tag.class  parameter. So the test shouldn't fail at all?! But it  
> does....any clue why? Am I getting the aNonNull functionality wrong?  
> It's the first time I'm using it.
>
> Thanks a lot for any help.
>
> Cheers,
> Thomas
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>

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



Re: Strange issue with aNonNull expectation

by Thomas Becker-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hey Steve,

thanks for the fast reply.

this one: one (tagDao).findTagByKeyForClientId(tagKey, clientId);  is a
check if a Tag with tagKey does already exist and the expected result is
null. That's why I do not have a will clause for that one. Maybe
will(returnValue(null) would be a cleaner and more readable solution. I
will change that, so it's clear for everyone.

Tag is a model class pojo. Actually this class follows the anaemic
antipattern and does only work as a data container with getters/setters.
Does aNonNull only work with interfaces?!

Cheers,
Thomas

Steve Freeman wrote:

> I'm confused too.
>
> The only thing I notice is that you seem to have some defaults set up
> on the tagDao, since the find method doesn't have a will() clause. Is
> that just a typo or is there more setup going on? Also, is Tag an
> interface or class and are there any overloaded methods?
>
> S.
>
> On 8 May 2008, at 08:25, Thomas Becker wrote:
>> Hi Usergroup,
>>
>> I'm using jmock more and more extensively in my Unit Test and I
>> really like it. Saves myself a bunch of work. However I got a strange
>> AssertionError which I do neither really understand nor do I get it
>> resolved.  Maybe you can shed some light on me.
>>
>> Here's the test method:
>>
>>   /**
>>    * Test method for {@link
>> net.netm.me.moritz.service.impl.TagServiceImpl#addTag(java.lang.String,
>> java.lang.String, int, Long)}.
>>    */
>>   @Test
>>   public void testAddGenreTag() throws TagAlreadyExistsException{
>>             // expectations
>>       context.checking(new Expectations() {{
>>            one (contractDao).findContract(contractId);
>>            will(returnValue(contract));
>>           one (tagDao).findTagByKeyForClientId(tagKey, clientId);
>>            one (tagTypeService).getTagType(genreTagType.getId());
>>            will (returnValue(genreTagType));
>>            one (tagDao).save(aNonNull(Tag.class));
>>       }});
>>             // method to test
>>       Tag newTag = tagService.addTag(tagKey, tagDescription,
>> genreTagType.getId(), contractId);
>>       assertTrue("new Genre Tag should start  with Genre",
>> newTag.getKey().startsWith("GENRE."));
>>   }
>>
>> This is the expectation which fails:
>> one (tagDao).save(aNonNull(Tag.class));
>>
>> And the FailureTrace:
>>
>> java.lang.AssertionError: unexpected invocation:
>> tagDao.save(<Tag[id=0,key=GENRE.Take
>> That,client.id=186,description=Tag for Artist Take
>> That,tagType.key=GENRE]>)
>> expectations:
>> expected exactly 1 time, already invoked 1 time:
>> contractDao.findContract(<21009747L>); returns
>> <net.netm.me.core.model.Contract@15664f1a[id=21009747,name=<null>]>
>> expected exactly 1 time, already invoked 1 time:
>> tagDao.findTagByKeyForClientId("Take That", <186L>); returns a
>> default value
>> expected exactly 1 time, already invoked 1 time:
>> tagTypeService.getTagType(<0>); returns <TagType[id=0,key=GENRE]>
>> expected exactly 1 time, never invoked: tagDao.save(<not null>);
>> returns a default value
>>   at
>> org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
>>
>>   at org.jmock.Mockery.dispatch(Mockery.java:204)
>>   at org.jmock.Mockery.access$000(Mockery.java:37)
>>   at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
>>   at
>> org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
>>   at
>> org.jmock.internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:36)
>>
>>   at
>> org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
>>
>>   at $Proxy7.save(Unknown Source)
>>   at
>> net.netm.me.moritz.service.impl.TagServiceImpl.createAndPersistTag(TagServiceImpl.java:257)
>>
>>   at
>> net.netm.me.moritz.service.impl.TagServiceImpl.addTag(TagServiceImpl.java:128)
>>
>>   at
>> net.netm.me.moritz.service.TagServiceTest.testAddGenreTag(TagServiceTest.java:262)
>>
>>   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>   at
>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>
>>   at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>
>>   at java.lang.reflect.Method.invoke(Method.java:597)
>>   at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
>>   at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:36)
>>   at
>> org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
>>
>>   at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
>>   at
>> org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
>>
>>   at
>> org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
>>   at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
>>   at
>> org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
>>
>>   at
>> org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
>>
>>   at
>> org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
>>
>>   at
>> org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
>>
>>   at
>> org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
>>   at
>> org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
>>
>>   at
>> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
>>
>>   at
>> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>>
>>   at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>>
>>   at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>>
>>   at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
>>
>>   at
>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
>>
>>
>> I excpect any Tag.class and tagDao.save gets called with a Tag.class  
>> parameter. So the test shouldn't fail at all?! But it does....any
>> clue why? Am I getting the aNonNull functionality wrong? It's the
>> first time I'm using it.
>>
>> Thanks a lot for any help.
>>
>> Cheers,
>> Thomas
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>
> 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
>
>

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

    http://xircles.codehaus.org/manage_email



Re: Strange issue with aNonNull expectation

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

As a first cut, I'd suggest adding the return clause -- although that  
shouldn't make a difference. (incidentally, I'd also suggest using the  
allowing() clause for the query methods, it's the same implementation  
but we think it's more expressive about the intentions of the test).

Sorry, when I said Tag I meant TagDao.

S.

On 8 May 2008, at 10:04, Thomas Becker wrote:

> Hey Steve,
>
> thanks for the fast reply.
>
> this one: one (tagDao).findTagByKeyForClientId(tagKey, clientId);  
> is a check if a Tag with tagKey does already exist and the expected  
> result is null. That's why I do not have a will clause for that one.  
> Maybe will(returnValue(null) would be a cleaner and more readable  
> solution. I will change that, so it's clear for everyone.
>
> Tag is a model class pojo. Actually this class follows the anaemic  
> antipattern and does only work as a data container with getters/
> setters. Does aNonNull only work with interfaces?!
>
> Cheers,
> Thomas
>
> Steve Freeman wrote:
>> I'm confused too.
>>
>> The only thing I notice is that you seem to have some defaults set  
>> up on the tagDao, since the find method doesn't have a will()  
>> clause. Is that just a typo or is there more setup going on? Also,  
>> is Tag an interface or class and are there any overloaded methods?
>>
>> S.
>>
>> On 8 May 2008, at 08:25, Thomas Becker wrote:
>>> Hi Usergroup,
>>>
>>> I'm using jmock more and more extensively in my Unit Test and I  
>>> really like it. Saves myself a bunch of work. However I got a  
>>> strange AssertionError which I do neither really understand nor do  
>>> I get it resolved.  Maybe you can shed some light on me.
>>>
>>> Here's the test method:
>>>
>>>  /**
>>>   * Test method for {@link  
>>> net
>>> .netm
>>> .me.moritz.service.impl.TagServiceImpl#addTag(java.lang.String,  
>>> java.lang.String, int, Long)}.
>>>   */
>>>  @Test
>>>  public void testAddGenreTag() throws TagAlreadyExistsException{
>>>            // expectations
>>>      context.checking(new Expectations() {{
>>>           one (contractDao).findContract(contractId);
>>>           will(returnValue(contract));
>>>          one (tagDao).findTagByKeyForClientId(tagKey, clientId);
>>>           one (tagTypeService).getTagType(genreTagType.getId());
>>>           will (returnValue(genreTagType));
>>>           one (tagDao).save(aNonNull(Tag.class));
>>>      }});
>>>            // method to test
>>>      Tag newTag = tagService.addTag(tagKey, tagDescription,  
>>> genreTagType.getId(), contractId);
>>>      assertTrue("new Genre Tag should start  with Genre",  
>>> newTag.getKey().startsWith("GENRE."));
>>>  }
>>>
>>> This is the expectation which fails:
>>> one (tagDao).save(aNonNull(Tag.class));
>>>
>>> And the FailureTrace:
>>>
>>> java.lang.AssertionError: unexpected invocation:  
>>> tagDao.save(<Tag[id=0,key=GENRE.Take  
>>> That,client.id=186,description=Tag for Artist Take  
>>> That,tagType.key=GENRE]>)
>>> expectations:
>>> expected exactly 1 time, already invoked 1 time:  
>>> contractDao.findContract(<21009747L>); returns  
>>> <net.netm.me.core.model.Contract@15664f1a[id=21009747,name=<null>]>
>>> expected exactly 1 time, already invoked 1 time:  
>>> tagDao.findTagByKeyForClientId("Take That", <186L>); returns a  
>>> default value
>>> expected exactly 1 time, already invoked 1 time:  
>>> tagTypeService.getTagType(<0>); returns <TagType[id=0,key=GENRE]>
>>> expected exactly 1 time, never invoked: tagDao.save(<not null>);  
>>> returns a default value
>>>  at  
>>> org
>>> .jmock
>>> .internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:
>>> 56)
>>>  at org.jmock.Mockery.dispatch(Mockery.java:204)
>>>  at org.jmock.Mockery.access$000(Mockery.java:37)
>>>  at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
>>>  at  
>>> org
>>> .jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:
>>> 27)
>>>  at  
>>> org
>>> .jmock
>>> .internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:
>>> 36)
>>>  at org.jmock.lib.JavaReflectionImposteriser
>>> $1.invoke(JavaReflectionImposteriser.java:33)
>>>  at $Proxy7.save(Unknown Source)
>>>  at  
>>> net
>>> .netm
>>> .me
>>> .moritz
>>> .service
>>> .impl.TagServiceImpl.createAndPersistTag(TagServiceImpl.java:257)
>>>  at  
>>> net
>>> .netm
>>> .me.moritz.service.impl.TagServiceImpl.addTag(TagServiceImpl.java:
>>> 128)
>>>  at  
>>> net
>>> .netm
>>> .me
>>> .moritz.service.TagServiceTest.testAddGenreTag(TagServiceTest.java:
>>> 262)
>>>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>  at  
>>> sun
>>> .reflect
>>> .NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>>  at  
>>> sun
>>> .reflect
>>> .DelegatingMethodAccessorImpl
>>> .invoke(DelegatingMethodAccessorImpl.java:25)
>>>  at java.lang.reflect.Method.invoke(Method.java:597)
>>>  at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
>>>  at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:36)
>>>  at  
>>> org
>>> .junit
>>> .internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
>>>  at org.junit.internal.runners.MethodRoadie
>>> $2.run(MethodRoadie.java:79)
>>>  at  
>>> org
>>> .junit
>>> .internal
>>> .runners
>>> .MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
>>>  at  
>>> org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
>>> 77)
>>>  at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:
>>> 42)
>>>  at  
>>> org
>>> .junit
>>> .internal
>>> .runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:
>>> 88)
>>>  at  
>>> org
>>> .junit
>>> .internal
>>> .runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
>>>  at org.junit.internal.runners.JUnit4ClassRunner
>>> $1.run(JUnit4ClassRunner.java:44)
>>>  at  
>>> org
>>> .junit
>>> .internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
>>>  at  
>>> org
>>> .junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:
>>> 37)
>>>  at  
>>> org
>>> .junit
>>> .internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
>>>  at  
>>> org
>>> .eclipse
>>> .jdt
>>> .internal
>>> .junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
>>>  at  
>>> org
>>> .eclipse
>>> .jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>>>  at  
>>> org
>>> .eclipse
>>> .jdt
>>> .internal
>>> .junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>>>  at  
>>> org
>>> .eclipse
>>> .jdt
>>> .internal
>>> .junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>>>  at  
>>> org
>>> .eclipse
>>> .jdt
>>> .internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:
>>> 386)
>>>  at  
>>> org
>>> .eclipse
>>> .jdt
>>> .internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:
>>> 196)
>>>
>>> I excpect any Tag.class and tagDao.save gets called with a  
>>> Tag.class  parameter. So the test shouldn't fail at all?! But it  
>>> does....any clue why? Am I getting the aNonNull functionality  
>>> wrong? It's the first time I'm using it.
>>>
>>> Thanks a lot for any help.
>>>
>>> Cheers,
>>> Thomas
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>  http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>> 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
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>   http://xircles.codehaus.org/manage_email
>
>

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



Re: Strange issue with aNonNull expectation

by Thomas Becker-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I know that this calls will happen exactly once. Isn't the one() clause
the correct one to use in my case? Why do you think allowing fits better
in my scenario? Don't get me wrong, I just do not understand why
allowing should be more expressive here. I'm happy for all suggestions
and implement them if I understand them.

I modified the expectation to explicitly return null as you suggested,
since it's more expressive:
            one (tagDao).findTagByKeyForClientId(tagKey, clientId);
            will(returnValue(null));

TagDao is an interface and the save() method is implemented with once
with only one signature: save(Object object).

Strange thing however is, that I'm doing a similar thing in another test
now, which just works fine. The only difference is that I'm using
saveOrUpdate now instead of save. So I tried to refactor the service
layer code to use saveOrUpdate in the other case as well. But it didn't
solve the assertion exception so I rolled the change back.

Here's where it works fine:

    @Test
    public void testUpdateTag() throws TagNotFoundException{
       
        // expectations
        context.checking(new Expectations() {{
            one (tagDao).findTagById(tagId);
             will(returnValue(tag));
            one(tagDao).saveOrUpdate(with(aNonNull(Tag.class)));
        }});
       
        // method to test
        Tag tag = tagService.updateTag(tagId, "newKey",
"newDescription", contractId);
        assertEquals("returned Tag should have newKey as tagKey",
"newKey", tag.getKey());
        assertEquals("returned Tag should have newDescription as
tagDescription", "newDescription", tag.getDescription());
    }

Steve Freeman wrote:

> As a first cut, I'd suggest adding the return clause -- although that
> shouldn't make a difference. (incidentally, I'd also suggest using the
> allowing() clause for the query methods, it's the same implementation
> but we think it's more expressive about the intentions of the test).
>
> Sorry, when I said Tag I meant TagDao.
>
> S.
>
> On 8 May 2008, at 10:04, Thomas Becker wrote:
>> Hey Steve,
>>
>> thanks for the fast reply.
>>
>> this one: one (tagDao).findTagByKeyForClientId(tagKey, clientId);  is
>> a check if a Tag with tagKey does already exist and the expected
>> result is null. That's why I do not have a will clause for that one.
>> Maybe will(returnValue(null) would be a cleaner and more readable
>> solution. I will change that, so it's clear for everyone.
>>
>> Tag is a model class pojo. Actually this class follows the anaemic
>> antipattern and does only work as a data container with
>> getters/setters. Does aNonNull only work with interfaces?!
>>
>> Cheers,
>> Thomas
>>
>> Steve Freeman wrote:
>>> I'm confused too.
>>>
>>> The only thing I notice is that you seem to have some defaults set
>>> up on the tagDao, since the find method doesn't have a will()
>>> clause. Is that just a typo or is there more setup going on? Also,
>>> is Tag an interface or class and are there any overloaded methods?
>>>
>>> S.
>>>
>>> On 8 May 2008, at 08:25, Thomas Becker wrote:
>>>> Hi Usergroup,
>>>>
>>>> I'm using jmock more and more extensively in my Unit Test and I
>>>> really like it. Saves myself a bunch of work. However I got a
>>>> strange AssertionError which I do neither really understand nor do
>>>> I get it resolved.  Maybe you can shed some light on me.
>>>>
>>>> Here's the test method:
>>>>
>>>>  /**
>>>>   * Test method for {@link
>>>> net.netm.me.moritz.service.impl.TagServiceImpl#addTag(java.lang.String,
>>>> java.lang.String, int, Long)}.
>>>>   */
>>>>  @Test
>>>>  public void testAddGenreTag() throws TagAlreadyExistsException{
>>>>            // expectations
>>>>      context.checking(new Expectations() {{
>>>>           one (contractDao).findContract(contractId);
>>>>           will(returnValue(contract));
>>>>          one (tagDao).findTagByKeyForClientId(tagKey, clientId);
>>>>           one (tagTypeService).getTagType(genreTagType.getId());
>>>>           will (returnValue(genreTagType));
>>>>           one (tagDao).save(aNonNull(Tag.class));
>>>>      }});
>>>>            // method to test
>>>>      Tag newTag = tagService.addTag(tagKey, tagDescription,
>>>> genreTagType.getId(), contractId);
>>>>      assertTrue("new Genre Tag should start  with Genre",
>>>> newTag.getKey().startsWith("GENRE."));
>>>>  }
>>>>
>>>> This is the expectation which fails:
>>>> one (tagDao).save(aNonNull(Tag.class));
>>>>
>>>> And the FailureTrace:
>>>>
>>>> java.lang.AssertionError: unexpected invocation:
>>>> tagDao.save(<Tag[id=0,key=GENRE.Take
>>>> That,client.id=186,description=Tag for Artist Take
>>>> That,tagType.key=GENRE]>)
>>>> expectations:
>>>> expected exactly 1 time, already invoked 1 time:
>>>> contractDao.findContract(<21009747L>); returns
>>>> <net.netm.me.core.model.Contract@15664f1a[id=21009747,name=<null>]>
>>>> expected exactly 1 time, already invoked 1 time:
>>>> tagDao.findTagByKeyForClientId("Take That", <186L>); returns a
>>>> default value
>>>> expected exactly 1 time, already invoked 1 time:
>>>> tagTypeService.getTagType(<0>); returns <TagType[id=0,key=GENRE]>
>>>> expected exactly 1 time, never invoked: tagDao.save(<not null>);
>>>> returns a default value
>>>>  at
>>>> org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
>>>>
>>>>  at org.jmock.Mockery.dispatch(Mockery.java:204)
>>>>  at org.jmock.Mockery.access$000(Mockery.java:37)
>>>>  at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
>>>>  at
>>>> org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
>>>>
>>>>  at
>>>> org.jmock.internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:36)
>>>>
>>>>  at
>>>> org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
>>>>
>>>>  at $Proxy7.save(Unknown Source)
>>>>  at
>>>> net.netm.me.moritz.service.impl.TagServiceImpl.createAndPersistTag(TagServiceImpl.java:257)
>>>>
>>>>  at
>>>> net.netm.me.moritz.service.impl.TagServiceImpl.addTag(TagServiceImpl.java:128)
>>>>
>>>>  at
>>>> net.netm.me.moritz.service.TagServiceTest.testAddGenreTag(TagServiceTest.java:262)
>>>>
>>>>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>  at
>>>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>>>
>>>>  at
>>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>>>
>>>>  at java.lang.reflect.Method.invoke(Method.java:597)
>>>>  at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
>>>>  at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:36)
>>>>  at
>>>> org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
>>>>
>>>>  at
>>>> org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
>>>>  at
>>>> org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
>>>>
>>>>  at
>>>> org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
>>>>  at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
>>>>  at
>>>> org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
>>>>
>>>>  at
>>>> org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
>>>>
>>>>  at
>>>> org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
>>>>
>>>>  at
>>>> org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
>>>>
>>>>  at
>>>> org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
>>>>
>>>>  at
>>>> org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
>>>>
>>>>  at
>>>> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
>>>>
>>>>  at
>>>> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>>>>
>>>>  at
>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>>>>
>>>>  at
>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>>>>
>>>>  at
>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
>>>>
>>>>  at
>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
>>>>
>>>>
>>>> I excpect any Tag.class and tagDao.save gets called with a
>>>> Tag.class  parameter. So the test shouldn't fail at all?! But it
>>>> does....any clue why? Am I getting the aNonNull functionality
>>>> wrong? It's the first time I'm using it.
>>>>
>>>> Thanks a lot for any help.
>>>>
>>>> Cheers,
>>>> Thomas
>>>>
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe from this list, please visit:
>>>>
>>>>  http://xircles.codehaus.org/manage_email
>>>>
>>>>
>>>
>>> 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
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>   http://xircles.codehaus.org/manage_email
>>
>>
>
> 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
>
>

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

    http://xircles.codehaus.org/manage_email



Re: Strange issue with aNonNull expectation

by Thomas Becker-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Steve, I just found this explanation in the cookbook and will refactor
the expectation to allow the query call.


    Expecting vs. Allowing

The most important distinction that cardinalities express is between
/expecting/ and /allowing/ an invocation.

If an invocation is allowed, it can occur during a test run, but the
test still passes if the invocation does not occur. If an invocation is
expected, on the other hand, it must occur during a test run; if it does
not occur, the test fails. When defining expectations you must choose
whether to expect or allow each invocation. In general, we find that
tests are kept flexible if you allow queries and expect commands. A
query is a method with no side effects that does nothing but query the
state of an object and a command is a method with side effects that may,
or may not, return a result. Of course, this does not hold all the time
but it's a useful rule of thumb to follow if there are no other
constraints on the number of times you expect a method to be called in
your test.



Thomas Becker wrote:

> I know that this calls will happen exactly once. Isn't the one()
> clause the correct one to use in my case? Why do you think allowing
> fits better in my scenario? Don't get me wrong, I just do not
> understand why allowing should be more expressive here. I'm happy for
> all suggestions and implement them if I understand them.
>
> I modified the expectation to explicitly return null as you suggested,
> since it's more expressive:
>            one (tagDao).findTagByKeyForClientId(tagKey, clientId);
>            will(returnValue(null));
>
> TagDao is an interface and the save() method is implemented with once
> with only one signature: save(Object object).
>
> Strange thing however is, that I'm doing a similar thing in another
> test now, which just works fine. The only difference is that I'm using
> saveOrUpdate now instead of save. So I tried to refactor the service
> layer code to use saveOrUpdate in the other case as well. But it
> didn't solve the assertion exception so I rolled the change back.
>
> Here's where it works fine:
>
>    @Test
>    public void testUpdateTag() throws TagNotFoundException{
>              // expectations
>        context.checking(new Expectations() {{
>            one (tagDao).findTagById(tagId);
>             will(returnValue(tag));
>            one(tagDao).saveOrUpdate(with(aNonNull(Tag.class)));
>        }});
>              // method to test
>        Tag tag = tagService.updateTag(tagId, "newKey",
> "newDescription", contractId);
>        assertEquals("returned Tag should have newKey as tagKey",
> "newKey", tag.getKey());
>        assertEquals("returned Tag should have newDescription as
> tagDescription", "newDescription", tag.getDescription());
>    }
>
> Steve Freeman wrote:
>> As a first cut, I'd suggest adding the return clause -- although that
>> shouldn't make a difference. (incidentally, I'd also suggest using
>> the allowing() clause for the query methods, it's the same
>> implementation but we think it's more expressive about the intentions
>> of the test).
>>
>> Sorry, when I said Tag I meant TagDao.
>>
>> S.
>>
>> On 8 May 2008, at 10:04, Thomas Becker wrote:
>>> Hey Steve,
>>>
>>> thanks for the fast reply.
>>>
>>> this one: one (tagDao).findTagByKeyForClientId(tagKey, clientId);  
>>> is a check if a Tag with tagKey does already exist and the expected
>>> result is null. That's why I do not have a will clause for that one.
>>> Maybe will(returnValue(null) would be a cleaner and more readable
>>> solution. I will change that, so it's clear for everyone.
>>>
>>> Tag is a model class pojo. Actually this class follows the anaemic
>>> antipattern and does only work as a data container with
>>> getters/setters. Does aNonNull only work with interfaces?!
>>>
>>> Cheers,
>>> Thomas
>>>
>>> Steve Freeman wrote:
>>>> I'm confused too.
>>>>
>>>> The only thing I notice is that you seem to have some defaults set
>>>> up on the tagDao, since the find method doesn't have a will()
>>>> clause. Is that just a typo or is there more setup going on? Also,
>>>> is Tag an interface or class and are there any overloaded methods?
>>>>
>>>> S.
>>>>
>>>> On 8 May 2008, at 08:25, Thomas Becker wrote:
>>>>> Hi Usergroup,
>>>>>
>>>>> I'm using jmock more and more extensively in my Unit Test and I
>>>>> really like it. Saves myself a bunch of work. However I got a
>>>>> strange AssertionError which I do neither really understand nor do
>>>>> I get it resolved.  Maybe you can shed some light on me.
>>>>>
>>>>> Here's the test method:
>>>>>
>>>>>  /**
>>>>>   * Test method for {@link
>>>>> net.netm.me.moritz.service.impl.TagServiceImpl#addTag(java.lang.String,
>>>>> java.lang.String, int, Long)}.
>>>>>   */
>>>>>  @Test
>>>>>  public void testAddGenreTag() throws TagAlreadyExistsException{
>>>>>            // expectations
>>>>>      context.checking(new Expectations() {{
>>>>>           one (contractDao).findContract(contractId);
>>>>>           will(returnValue(contract));
>>>>>          one (tagDao).findTagByKeyForClientId(tagKey, clientId);
>>>>>           one (tagTypeService).getTagType(genreTagType.getId());
>>>>>           will (returnValue(genreTagType));
>>>>>           one (tagDao).save(aNonNull(Tag.class));
>>>>>      }});
>>>>>            // method to test
>>>>>      Tag newTag = tagService.addTag(tagKey, tagDescription,
>>>>> genreTagType.getId(), contractId);
>>>>>      assertTrue("new Genre Tag should start  with Genre",
>>>>> newTag.getKey().startsWith("GENRE."));
>>>>>  }
>>>>>
>>>>> This is the expectation which fails:
>>>>> one (tagDao).save(aNonNull(Tag.class));
>>>>>
>>>>> And the FailureTrace:
>>>>>
>>>>> java.lang.AssertionError: unexpected invocation:
>>>>> tagDao.save(<Tag[id=0,key=GENRE.Take
>>>>> That,client.id=186,description=Tag for Artist Take
>>>>> That,tagType.key=GENRE]>)
>>>>> expectations:
>>>>> expected exactly 1 time, already invoked 1 time:
>>>>> contractDao.findContract(<21009747L>); returns
>>>>> <net.netm.me.core.model.Contract@15664f1a[id=21009747,name=<null>]>
>>>>> expected exactly 1 time, already invoked 1 time:
>>>>> tagDao.findTagByKeyForClientId("Take That", <186L>); returns a
>>>>> default value
>>>>> expected exactly 1 time, already invoked 1 time:
>>>>> tagTypeService.getTagType(<0>); returns <TagType[id=0,key=GENRE]>
>>>>> expected exactly 1 time, never invoked: tagDao.save(<not null>);
>>>>> returns a default value
>>>>>  at
>>>>> org.jmock.internal.InvocationDispatcher.dispatch(InvocationDispatcher.java:56)
>>>>>
>>>>>  at org.jmock.Mockery.dispatch(Mockery.java:204)
>>>>>  at org.jmock.Mockery.access$000(Mockery.java:37)
>>>>>  at org.jmock.Mockery$MockObject.invoke(Mockery.java:246)
>>>>>  at
>>>>> org.jmock.internal.InvocationDiverter.invoke(InvocationDiverter.java:27)
>>>>>
>>>>>  at
>>>>> org.jmock.internal.ProxiedObjectIdentity.invoke(ProxiedObjectIdentity.java:36)
>>>>>
>>>>>  at
>>>>> org.jmock.lib.JavaReflectionImposteriser$1.invoke(JavaReflectionImposteriser.java:33)
>>>>>
>>>>>  at $Proxy7.save(Unknown Source)
>>>>>  at
>>>>> net.netm.me.moritz.service.impl.TagServiceImpl.createAndPersistTag(TagServiceImpl.java:257)
>>>>>
>>>>>  at
>>>>> net.netm.me.moritz.service.impl.TagServiceImpl.addTag(TagServiceImpl.java:128)
>>>>>
>>>>>  at
>>>>> net.netm.me.moritz.service.TagServiceTest.testAddGenreTag(TagServiceTest.java:262)
>>>>>
>>>>>  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>>>>  at
>>>>> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
>>>>>
>>>>>  at
>>>>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
>>>>>
>>>>>  at java.lang.reflect.Method.invoke(Method.java:597)
>>>>>  at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:59)
>>>>>  at org.jmock.integration.junit4.JMock$1.invoke(JMock.java:36)
>>>>>  at
>>>>> org.junit.internal.runners.MethodRoadie.runTestMethod(MethodRoadie.java:98)
>>>>>
>>>>>  at
>>>>> org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:79)
>>>>>  at
>>>>> org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:87)
>>>>>
>>>>>  at
>>>>> org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:77)
>>>>>  at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
>>>>>  at
>>>>> org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod(JUnit4ClassRunner.java:88)
>>>>>
>>>>>  at
>>>>> org.junit.internal.runners.JUnit4ClassRunner.runMethods(JUnit4ClassRunner.java:51)
>>>>>
>>>>>  at
>>>>> org.junit.internal.runners.JUnit4ClassRunner$1.run(JUnit4ClassRunner.java:44)
>>>>>
>>>>>  at
>>>>> org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:27)
>>>>>
>>>>>  at
>>>>> org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:37)
>>>>>
>>>>>  at
>>>>> org.junit.internal.runners.JUnit4ClassRunner.run(JUnit4ClassRunner.java:42)
>>>>>
>>>>>  at
>>>>> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
>>>>>
>>>>>  at
>>>>> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>>>>>
>>>>>  at
>>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
>>>>>
>>>>>  at
>>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
>>>>>
>>>>>  at
>>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
>>>>>
>>>>>  at
>>>>> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
>>>>>
>>>>>
>>>>> I excpect any Tag.class and tagDao.save gets called with a
>>>>> Tag.class  parameter. So the test shouldn't fail at all?! But it
>>>>> does....any clue why? Am I getting the aNonNull functionality
>>>>> wrong? It's the first time I'm using it.
>>>>>
>>>>> Thanks a lot for any help.
>>>>>
>>>>> Cheers,
>>>>> Thomas
>>>>>
>>>>>
>>>>> ---------------------------------------------------------------------
>>>>> To unsubscribe from this list, please visit:
>>>>>
>>>>>  http://xircles.codehaus.org/manage_email
>>>>>
>>>>>
>>>>
>>>> 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
>>>>
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe from this list, please visit:
>>>
>>>   http://xircles.codehaus.org/manage_email
>>>
>>>
>>
>> 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
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

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

    http://xircles.codehaus.org/manage_email



Re: Strange issue with aNonNull expectation

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Good. Deep down it's the same mechanism, but we find it makes the  
tests clearer and a little less brittle.

S.

On 8 May 2008, at 17:35, Thomas Becker wrote:

> Steve, I just found this explanation in the cookbook and will  
> refactor the expectation to allow the query call.
>
>
>   Expecting vs. Allowing
>
> The most important distinction that cardinalities express is  
> between /expecting/ and /allowing/ an invocation.
>
> If an invocation is allowed, it can occur during a test run, but the  
> test still passes if the invocation does not occur. If an invocation  
> is expected, on the other hand, it must occur during a test run; if  
> it does not occur, the test fails. When defining expectations you  
> must choose whether to expect or allow each invocation. In general,  
> we find that tests are kept flexible if you allow queries and expect  
> commands. A query is a method with no side effects that does nothing  
> but query the state of an object and a command is a method with side  
> effects that may, or may not, return a result. Of course, this does  
> not hold all the time but it's a useful rule of thumb to follow if  
> there are no other constraints on the number of times you expect a  
> method to be called in your test.
>

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



Re: Strange issue with aNonNull expectation

by Steve Freeman-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I wonder if it's trying to match the method with a signature of a Tag  
argument and so is missing the signature with an Object argument? Try  
aNonNull(Object.class) and see if that works. Do you have template  
type checking turned off in the test method or class? You could also  
try the hamcrest matcher notNullValue().


S.

On 8 May 2008, at 17:32, Thomas Becker wrote:

> I know that this calls will happen exactly once. Isn't the one()  
> clause the correct one to use in my case? Why do you think allowing  
> fits better in my scenario? Don't get me wrong, I just do not  
> understand why allowing should be more expressive here. I'm happy  
> for all suggestions and implement them if I understand them.
>
> I modified the expectation to explicitly return null as you  
> suggested, since it's more expressive:
>           one (tagDao).findTagByKeyForClientId(tagKey, clientId);
>           will(returnValue(null));
>
> TagDao is an interface and the save() method is implemented with  
> once with only one signature: save(Object object).
>
> Strange thing however is, that I'm doing a similar thing in another  
> test now, which just works fine. The only difference is that I'm  
> using saveOrUpdate now instead of save. So I tried to refactor the  
> service layer code to use saveOrUpdate in the other case as well.  
> But it didn't solve the assertion exception so I rolled the change  
> back.
>
> Here's where it works fine:
>
>   @Test
>   public void testUpdateTag() throws TagNotFoundException{
>             // expectations
>       context.checking(new Expectations() {{
>           one (tagDao).findTagById(tagId);
>            will(returnValue(tag));
>           one(tagDao).saveOrUpdate(with(aNonNull(Tag.class)));
>       }});
>             // method to test
>       Tag tag = tagService.updateTag(tagId, "newKey",  
> "newDescription", contractId);
>       assertEquals("returned Tag should have newKey as tagKey",  
> "newKey", tag.getKey());
>       assertEquals("returned Tag should have newDescription as  
> tagDescription", "newDescription", tag.getDescription());
>   }
>
> Steve Freeman wrote:
>> As a first cut, I'd suggest adding the return clause -- although  
>> that shouldn't make a difference. (incidentally, I'd also suggest  
>> using the allowing() clause for the query methods, it's the same  
>> implementation but we think it's more expressive about the  
>> intentions of the test).
>>
>> Sorry, when I said Tag I meant TagDao.
>>
>> S.
>>
>> On 8 May 2008, at 10:04, Thomas Becker wrote:
>>> Hey Steve,
>>>
>>> thanks for the fast reply.
>>>
>>> this one: one (tagDao).findTagByKeyForClientId(tagKey, clientId);  
>>> is a check if a Tag with tagKey does already exist and the  
>>> expected result is null. That's why I do not have a will clause  
>>> for that one. Maybe will(returnValue(null) would be a cleaner and  
>>> more readable solution. I will change that, so it's clear for  
>>> everyone.
>>>
>>> Tag is a model class pojo. Actually this class follows the anaemic  
>>> antipattern and does only work as a data container with getters/
>>> setters. Does aNonNull only work with interfaces?!
>>>
>>> Cheers,
>>> Thomas
>>>
>>> Steve Freeman wrote:
>>>> I'm confused too.
>>>>
>>>> The only thing I notice is that you seem to have some defaults  
>>>> set up on the tagDao, since the find method doesn't have a will()  
>>>> clause. Is that just a typo or is there more setup going on?  
>>>> Also, is Tag an interface or class and are there any overloaded  
>>>> methods?
>>>>
>>>> S.
>>>>
>>>> On 8 May 2008, at 08:25, Thomas Becker wrote:
>>>>> Hi Usergroup,
>>>>>
>>>>> I'm using jmock more and more extensively in my Unit Test and I  
>>>>> really like it. Saves myself a bunch of work. However I got a  
>>>>> strange AssertionError which I do neither really understand nor  
>>>>> do I get it resolved.  Maybe you can shed some light on me.
>>>>>
>>>>> Here's the test method:
>>>>>
>>>>> /**
>>>>>  * Test method for {@link  
>>>>> net
>>>>> .netm
>>>>> .me.moritz.service.impl.TagServiceImpl#addTag(java.lang.String,  
>>>>> java.lang.String, int, Long)}.
>>>>>  */
>>>>> @Test
>>>>> public void testAddGenreTag() throws TagAlreadyExistsException{
>>>>>           // expectations
>>>>>     context.checking(new Expectations() {{
>>>>>          one (contractDao).findContract(contractId);
>>>>>          will(returnValue(contract));
>>>>>         one (tagDao).findTagByKeyForClientId(tagKey, clientId);
>>>>>          one (tagTypeService).getTagType(genreTagType.getId());
>>>>>          will (returnValue(genreTagType));
>>>>>          one (tagDao).save(aNonNull(Tag.class));
>>>>>     }});
>>>>>           // method to test
>>>>>     Tag newTag = tagService.addTag(tagKey, tagDescription,  
>>>>> genreTagType.getId(), contractId);
>>>>>     assertTrue("new Genre Tag should start  with Genre",  
>>>>> newTag.getKey().startsWith("GENRE."));
>>>>> }
>>>>>
>>>>> This is the expectation which fails:
>>>>> one (tagDao).save(aNonNull(Tag.class));
>>>>>
>>>>> And the FailureTrace:
>>>>>
>>>>> java.lang.AssertionError: unexpected invocation:  
>>>>> tagDao.save(<Tag[id=0,key=GENRE.Take  
>>>>> That,client.id=186,description=Tag