• 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
  • Tim Cooke
  • Ron McLeod
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Mock in Junit

 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In Junit, when the function in other class is available/callerable, do we need to use mock for the other class? I think we can use mock to isolate the test focus on the own class itself. Now I see some code in existing production repository call the other function directly, try to understand which case we should use mock, or some other case we should call the fuction in other class directly, Or maybe existing code is not good way, we should always use mock for outside class?

Thanks
 
Marshal
Posts: 6197
500
IntelliJ IDE Python TypeScript Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Moderator Hat)
Moved topic to Testing Forum

(Programmer Hat)
Whether you use a Mock object or a real object as a collaborator for a class under test is entirely up to you. There are no rules to say one is right and one is wrong.

Mock frameworks exist to help you test classes that have collaborators that are difficult to instantiate in a test harness. This could be because you have a cascade of dependencies causing you to have to instantiate half the world, or because the collaborating class has dependencies to external resources, such as a database or some other resource that requires particular network access for example.

Personally I do try and use real objects where possible, but if it ends up with a 'setup sermon' to instantiate the thing then I might swap it with a Fake to make my test easier or more understandable.
 
Yup, yup, yup. Tiny ad:
The new gardening playing cards kickstarter is now live!
https://www.kickstarter.com/projects/paulwheaton/garden-cards
reply
    Bookmark Topic Watch Topic
  • New Topic