« Return to Thread: Strange issue with aNonNull expectation

Strange issue with aNonNull expectation

by Thomas Becker-5 :: Rate this Message:

Reply to Author | View in Thread

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


 « Return to Thread: Strange issue with aNonNull expectation

LightInTheBox - Buy quality products at wholesale price!