• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mock Objects for easymock -- Assertion failure Error

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I am new to Mock Objects. I wrote Mock test cases for my BIZ layer functionality, i am getting this error if i test the scenario which verifies that a respective method of my Model Object is being called or not ???

i'm putting the assertionFaliureError i got while testing .

junit.framework.AssertionFailedError:
Unexpected method call listCampaignNames():
listCampaignNames(): expected: 0, actual: 1
at org.easymock.MockControl$4.invoke(MockControl.java:148)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
at $Proxy1.listCampaignNames(Unknown Source)
at com.sgcib.agora.business.economic.tfv.discrepancy.biz.TFVDiscrepancyTypesBizService.getTFVModelList(TFVDiscrepancyTypesBizService.java:93)
at com.sgcib.agora.business.economic.tfv.discrepancy.biz.TFVDiscrepancyBizServiceTest.testGetAllModelNamesCollection(TFVDiscrepancyBizServiceTest.java:80)
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:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
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)


Thanks in Advance,
Hareendra.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I got to know by this message that you were expecting "1" but got "0". So, it means you need to fix your code. Or is there something else? Can you show us the test code?
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Adeel,

Thanks for the reply.

public void testGetAllDiscrepancyTypes() {
try {
TFVDiscrepancyTypesBizService bizService = new TFVDiscrepancyTypesBizService();
bizService.setDAOServices(daoServices);

daoServices.getTFVDiscrepancyTypesDAO();
daoServicesControl.setReturnValue(tfvDiscrepancyDAO);

ITFVDiscrepancyTypesCriteria criteria = new TFVDiscrepancyTypesCriteria();
tfvDiscrepancyDAO.getAllDiscrepency(criteria);
tfvDiscrepancyDAOControl.setReturnValue(new ArrayList());

daoServicesControl.replay();
tfvDiscrepancyDAOControl.replay();

bizService.getAllDiscrepancyTypes(criteria);

tfvDiscrepancyDAOControl.verify();
}
catch (Exception e) {
e.printStackTrace();
fail("Unexpected Exception "+e.getMessage());
}
}

this is the code i am using for running the scenario.
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
above scenario is running successfully. below scenario is not running successfully.

public void testGetAllModelNamesCollection() {
try {
TFVDiscrepancyTypesBizService bizService = new TFVDiscrepancyTypesBizService();
bizService.setDAOServices(daoServices);

daoServices.getTfvCampaignViewDAO();
daoServicesControl.setReturnValue(tfvModelDAO);

ITfvCampaignViewDTO criteria = new TfvCampaignViewDTO();
tfvModelDAO.listCampaignNames(criteria);
tfvModelDAOControl.setReturnValue(new ArrayList());

daoServicesControl.replay();
tfvModelDAOControl.replay();

bizService.getTFVModelList();

tfvModelDAOControl.verify();
}
catch (Exception e) {
e.printStackTrace();
fail("Unexpected Exception "+e.getMessage());
}
}

Thanks in Advance,
Hareendra
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assuming this last example goes with the error message in your first post, easyMock is telling you that the class under test calls "listCampaignNames" while the test never registers that method with the mock.
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thanks for your help.

could you please lookk into another issue with the below test scenario.

public void testInsertCreatedDiscrepancy() {
try {
TFVDiscrepancyTypesBizService bizService = new TFVDiscrepancyTypesBizService();
bizService.setDAOServices(daoServices);

daoServices.getTFVDiscrepancyTypesDAO();
daoServicesControl.setReturnValue(tfvDiscrepancyDAO);

ITFVDiscrepancyTypesDTO criteria = new TFVDiscrepancyTypesDTO();
tfvDiscrepancyDAO.insertDiscrepancy(criteria);

daoServicesControl.replay();
tfvDiscrepancyDAOControl.replay();

bizService.insertCreatedDiscrepancy(criteria);

tfvDiscrepancyDAOControl.verify();
} catch (Exception e) {
e.printStackTrace();
fail("Unexpected Exception " + e.getMessage());
}
}

i'm getting error for the above scenario.

junit.framework.AssertionFailedError:
Unexpected method call insertDiscrepancy(null):
insertDiscrepancy(null): expected: 0, actual: 1
insertDiscrepancy(null): expected: 1, actual: 0
at org.easymock.MockControl$4.invoke(MockControl.java:148)
at org.easymock.internal.ObjectMethodsFilter.invoke(ObjectMethodsFilter.java:44)
at $Proxy2.insertDiscrepancy(Unknown Source)
at com.sgcib.agora.business.economic.tfv.discrepancy.biz.TFVDiscrepancyTypesBizService.insertCreatedDiscrepancy(TFVDiscrepancyTypesBizService.java:118)
at com.sgcib.agora.business.economic.tfv.discrepancy.biz.TFVDiscrepancyBizServiceTest.testInsertCreatedDiscrepancy(TFVDiscrepancyBizServiceTest.java:134)
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:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
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)


Thanks in Advance,
Hareendra.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see a connection between the test and stack trace. The error complains about a null being passed. But in the test, it clearly isn't null.
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Thanks for your Reply,

even me also not able to trace this . Can any one suggest me for geeting thru this.

Thanks in Advance
 
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does TFVDiscrepancyTypesDTO implement the equals() method?
 
Hareendranath Babu Kotha
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes TFVDiscrepancyTypesDTO implements equals method.
 
Lasse Koskela
author
Posts: 11962
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Hareendranath Babu Kotha:
Yes TFVDiscrepancyTypesDTO implements equals method.


Hmm. That's curious. I thought the culprit might be that the equals() method isn't implemented and what EasyMock was complaining about...

insertDiscrepancy(null): expected: 0, actual: 1
insertDiscrepancy(null): expected: 1, actual: 0

...was really the one and the same method call.

Usually when you see this pattern (one expected and one unexpected invocation for the same method) it's a case of the arguments not matching according to an equals() comparison.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have set up the expectation that TfvDiscrepancyDAO.insertDiscrepancy will be called, but it isn't. Is this the class which does not override equals()?
 
Hey, sticks and stones baby. And maybe a wee mention of my stuff:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic