Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

setting expectations on void methods

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Say i have two Classes

1) ASingleton class which implements ASingletonInterface
2)BUsesSingleton class
Now lets see what methods each class have

-->ASingeltonInterface only has void writeToCSV() function,which is implemented by ASingletonClass
-->ASingleton has a static method getInstance(), and a method void writeToCSV().
-->BUsesSingelton has a update() method, which in turn uses writeToCSV() function.


void writeToCSV() does nothing but just writes to a file

SUT in this case is BUsesSingelton
I tried the following scenrios but my test case always fails,Basicaaly i am chekcing for an exception to occur(Using junit 4 and easy mock)



Above test case always fail though i expect this to pass(I expect i should get MyException).However, my test fails saying "assertion error expected MyException").

I am using expectLastCall() instead of expect()since writeTOCSV() is returning void.
Please tell me where am i wrong
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not familiar with Easymock, but are you sure that your fixture is set up properly and the singleton is returning the mocked object..?
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes iam sure.....it returns a singleton
 
Lorand Komaromi
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question was a bit imprecise, what I wanted to ask: are you sure that when BUsesSingleton.uodate() is invoked in your test method, ASingleton.getInstance() returns the mock instance on which you set the expectations..?
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes iam sure that ASingleton.getInstance() returns the mock instance on which i set the expectations.I cross verified with the reference id(memory ref) as well.

i get an exception which says

above exception comes in update() method of BUsesSingleton.i.e when it reaches writeToCSV()
 
Maan Suraj
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Lorand

your question helped me solve my issues.

As metnioed, there were couple of issues.

1)rightly identifed by you,ASingleton.getInstance() didn't returned the mock instance on which i set the expectations.
2)However, even after doing so, i was still getting assertion error.As mentioned in previous post(above one), i was getting eroor with ARecord. Then i realised that i had to inject Arecord as well. :-)


Thanks again


 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic