Java Mailing List Archive

http://www.gg3721.com/

Home » user.jmock »

[jmock-user] strange jmock behaviour in case of different results expected for same function for different parameters

Ralph Urban

2010-01-05

Replies: Find Java Web Hosting

Author LoginPost Reply
Hi,

I am mocking a bean. Its function getPackages is called twice, both calls with
diferent parameters. The function should return diffent lists. Somehow JMock
merges these lists. Did I specifiy wrongly?

Test1:
  @Test
  public void testStrangeMock() {
    final List<String> packages2 = new ArrayList<String>();
    packages2.add("de.psi.telco.mccm");
    final List<String> packages = new ArrayList<String>();
    packages.add("de.psi.telco.mccm.test1");
    packages.add("de.psi.telco.mccm.test2");
    mMockery.checking(new Expectations() {{
       allowing (mPropS).getPackages(with(equal("de.psi.telco.mccm")),
            with(equal(true)));
          will(returnValue(packages));
          allowing (mPropS).getPackages(with(equal("de.psi.telco")),
             with(equal(false)));
          will(returnValue(packages2));
    }});
   
    Map<String,AbstractMCCMProperty> results = mLdap.getPropertiesMap(
          "dc=mccm,dc=telco,dc=psi.de",
          "dc=test1");
  }

Result1:
Testcase: testStrangeMock took 0,005 sec
 Caused an ERROR
unexpected invocation:
propertyStorageLocal.getProperties("de.psi.telco.mccm.test2")
expectations:
allowed, already invoked 1 time: propertyStorageLocalHome.create(); returns
<propertyStorageLocal>
allowed, never invoked: propertyStorageLocal.remove(); returns a default value
allowed, already invoked 1 time:
propertyStorageLocal.getPackages("de.psi.telco.mccm", <true>); returns
<[de.psi.telco.mccm.test1, de.psi.telco.mccm.test2, de.psi.telco.mccm]>
allowed, already invoked 1 time:
propertyStorageLocal.getPackages("de.psi.telco", <false>); returns
<[de.psi.telco.mccm]>
what happened before this:
propertyStorageLocalHome.create()
propertyStorageLocal.getPackages("de.psi.telco.mccm", <true>)
propertyStorageLocal.getPackages("de.psi.telco", <false>)

Now the problem is in the line:
propertyStorageLocal.getPackages("de.psi.telco.mccm", <true>); returns
<[de.psi.telco.mccm.test1, de.psi.telco.mccm.test2, de.psi.telco.mccm]>
if you look in the testcase the List had only 2 elements not 3. To make this
clearer I use an empty List:

Test2:
  public void testStrangeMock() {
    final List<String> packages2 = new ArrayList<String>();
    packages2.add("de.psi.telco.mccm");
    mMockery.checking(new Expectations() {{
       allowing (mPropS).getPackages(with(equal("de.psi.telco.mccm")),
            with(equal(true)));
          will(returnValue(new ArrayList<String>()));
          allowing (mPropS).getPackages(with(equal("de.psi.telco")),
             with(equal(false)));
          will(returnValue(packages2));
    }});
   
    Map<String,AbstractMCCMProperty> results = mLdap.getPropertiesMap(
          "dc=mccm,dc=telco,dc=psi.de",
          "dc=test1");
  }

Result2:
Testcase: testStrangeMock took 0,006 sec
 Caused an ERROR
unexpected invocation: propertyStorageLocal.getProperties("de.psi.telco.mccm")
expectations:
allowed, already invoked 1 time: propertyStorageLocalHome.create(); returns
<propertyStorageLocal>
allowed, never invoked: propertyStorageLocal.remove(); returns a default value
allowed, already invoked 1 time:
propertyStorageLocal.getPackages("de.psi.telco.mccm", <true>); returns
<[de.psi.telco.mccm]>
allowed, already invoked 1 time:
propertyStorageLocal.getPackages("de.psi.telco", <false>); returns
<[de.psi.telco.mccm]>
what happened before this:
propertyStorageLocalHome.create()
propertyStorageLocal.getPackages("de.psi.telco.mccm", <true>)
propertyStorageLocal.getPackages("de.psi.telco", <false>)

even stranger, when I play around a bit and change the second parameter from
false to true the other call returns the correct value:

Test3:
  public void testStrangeMock() {
    final List<String> packages2 = new ArrayList<String>();
    packages2.add("de.psi.telco.mccm");
    mMockery.checking(new Expectations() {{
       allowing (mPropS).getPackages(with(equal("de.psi.telco.mccm")),
            with(equal(true)));
          will(returnValue(new ArrayList<String>()));
          allowing (mPropS).getPackages(with(equal("de.psi.telco")),
             with(equal(true)));
          will(returnValue(packages2));
    }});
   
    Map<String,AbstractMCCMProperty> results = mLdap.getPropertiesMap(
          "dc=mccm,dc=telco,dc=psi.de",
          "dc=test1");
  }

Result3:
Testcase: testStrangeMock took 0,005 sec
 Caused an ERROR
unexpected invocation: propertyStorageLocal.getPackages("de.psi.telco", <false>)
expectations:
allowed, already invoked 1 time: propertyStorageLocalHome.create(); returns
<propertyStorageLocal>
allowed, never invoked: propertyStorageLocal.remove(); returns a default value
allowed, already invoked 1 time:
propertyStorageLocal.getPackages("de.psi.telco.mccm", <true>); returns <[]>
allowed, never invoked: propertyStorageLocal.getPackages("de.psi.telco",
<true>); returns <[de.psi.telco.mccm]>
what happened before this:
propertyStorageLocalHome.create()
propertyStorageLocal.getPackages("de.psi.telco.mccm", <true>)

Anyone with an idea?

Regards, Ralph



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

  http://xircles.codehaus.org/manage_email


©2008 gg3721.com - Jax Systems, LLC, U.S.A.