Mock in Junit
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
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
Thanks
posted 11 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
(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.
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.
Tim Driven Development | Test until the fear goes away
| Yup, yup, yup. Tiny ad: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |












