• 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

Testing Singleton class using easy mock

 
Ranch Hand
Posts: 299
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a singleton class which after acquiring the object(single) writes to a csv file.I thought of writing unit test case for this singleton.However, I was not sure how to test this class using easy mock or junit.
The test cases which I thought were
1)Test this class to ensure that it creates only one object.
2)Also, this silgetong writes to a csv file, so i though i will test that as well.

But I was not sure wht else i can test.

Also, how to test the case 1), i.e to ensure that it creates only one object
 
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
Maan,
Those are good test cases. For the one object one, JUnit provides an assertSame method. You can call getInstance() twice on your singleteon and then assert the same object is returned.
 
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 Jeanne.

Also, this singleton writes to a csv file.The method which writes to the csv file is synchronized.I want to test the synchronized method. I want to create multiple threads and test this method.However,I dont want to write to the real file(since it may interfre when i deploy the code in production).I want to just mock the File and write to it.
I am not getting how to do it.
Please help
 
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
Your test machine probably can't write to the real production file anyway. Just in case, you can use a different file name. You can inject the String name to use in from the test. Similar to how we do so with mock objects. If you post a bit of code on how you use the String, I can be more specific.
 
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


This is the code snippet. Please let me know your test case.
 
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
One option is to create a helper method. The unit test could use a real file with a different stream or a ByteArrayOutputStream which writes to memory.


Another option is to do the substitution at a higher level - where you create the PrintStream. I'd lean towards this option because it mocks the stream everywhere not just in this method.
 
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
sorry Jeanne , am not getting your second option(i;e writing to print stream). could you please elaborate.

Also, i have opened another thread in this forum for a query related to testing other methods dependent on this "writeToCSV" method.I need your expertise there as well.

 
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

Another option is to do the substitution at a higher level - where you create the PrintStream. I'd lean towards this option because it mocks the stream everywhere not just in this method.



sorry Jeanne , am not getting your second option(i;e writing to print stream...above one ). could you please elaborate.

Also, i have opened another thread in this forum for a query related to testing other methods dependent on this "writeToCSV" method.I need your expertise there as well.(The thread title is "setting expectations on void methods")

 
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
When you create your PrintStream, you are probably doing something like:


What if you injected a different stream? In particular:
 
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
oh...ok got it Jeanne....Thanks...let me try with this
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic