| Author |
Mock Objects for easymock -- Assertion failure Error
|
Hareendranath Babu Kotha
Ranch Hand
Joined: May 22, 2006
Posts: 40
|
|
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.
|
Hari.K,<br />+919886082889
|
 |
Adeel Ansari
Ranch Hand
Joined: Aug 15, 2004
Posts: 2874
|
|
|
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
Joined: May 22, 2006
Posts: 40
|
|
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
Joined: May 22, 2006
Posts: 40
|
|
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
|
 |
Jeanne Boyarsky
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
|
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.
|
[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
|
 |
Hareendranath Babu Kotha
Ranch Hand
Joined: May 22, 2006
Posts: 40
|
|
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
internet detective
Marshal
Joined: May 26, 2003
Posts: 26184
|
|
|
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
Joined: May 22, 2006
Posts: 40
|
|
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
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
|
Does TFVDiscrepancyTypesDTO implement the equals() method?
|
Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
|
 |
Hareendranath Babu Kotha
Ranch Hand
Joined: May 22, 2006
Posts: 40
|
|
|
Yes TFVDiscrepancyTypesDTO implements equals method.
|
 |
Lasse Koskela
author
Sheriff
Joined: Jan 23, 2002
Posts: 11962
|
|
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.
|
 |
Roger Chung-Wee
Ranch Hand
Joined: Sep 29, 2002
Posts: 1683
|
|
|
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()?
|
SCJP 1.4, SCWCD 1.3, SCBCD 1.3
|
 |
 |
|
|
subject: Mock Objects for easymock -- Assertion failure Error
|
|
|