• 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 method to JUnit test

 
Greenhorn
Posts: 16
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi have the following method:



For this previous method I have the following Mock:






Now I have the following method:





How can I mock this method?? I did the following but I'm most certainly sure that this is not correct. Because of the parameter "id". I'm not using it,


 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not clear what you're trying to do. You want to create a mock, such that calls to getList(int) in the code you're testing actually call getList() with no parameters? I don't think that's possible, but of course you can mock getList(int) and have it call getList().
 
Daniel Afonso
Greenhorn
Posts: 16
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I edited the topic.
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If your mock's purpose is to bypass the database, then you have to get the data from somewhere else. In your first mock, you load it up from a "things" object, but you don't show where that object is created or populated. In any case, I would have your second method go through "things", pull out the members that match "id", and then return those as a list.
 
Daniel Afonso
Greenhorn
Posts: 16
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then my approach in the second method is correct?
 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, mocks are used for testing, so if it adequately tests your logic, then it is correct in that sense. I assumed that you would want to filter "things" by the id parameter to better mimic what the original method does. Your second mock doesn't do that, so in the sense of following my suggestion, no, it isn't correct.
 
Daniel Afonso
Greenhorn
Posts: 16
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How would you implemented this method filtering it by id?

thanks, i'm a rookie in tests using mocks
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your test case? Is it really necessary for your mock to filter a bunch of test "things" in a manner analogous to what is done by the working application? If so, it should be a trivial matter as Greg indicated to iterate over the map's contents and return those things which have the required id. However you might consider whether perhaps your test is attempting to handle more responsibility than is necessary. If the DAO layer has its own suite of tests that cover this same logic, then really it ought to be sufficient to verify that a call was made to the DAO, and that Something was returned by the service. (Of course if the service has additional logic to perform, the test should cover that too.)
 
I am displeased. You are no longer allowed to read this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic