Using mocks to set up object even if you will not be mocking any behavior or verifying any interaction with it?
Posted
by
smp7d
on Programmers
See other posts from Programmers
or by smp7d
Published on 2014-04-17T19:50:31Z
Indexed on
2014/06/02
21:45 UTC
Read the original article
Hit count: 198
mocking
When building a unit test, is it appropriate to use a mocking tool to assist you in setting up an object even if you will not be mocking any behavior or verifying any interaction with that object?
Here is a simple example in pseudo-code:
//an object we actually want to mock
Object someMockedObject = Mock(Object.class);
EqualityChecker checker = new EqualityChecker(someMockedObject);
//an object we are mocking only to avoid figuring out how to instantiate or
//tying ourselves to some constructor that may be removed in the future
ComplicatedObject someObjectThatIsHardToInstantiate = Mock(ComplicatedObject.class);
//set the expectation on the mock
When(someMockedObject).equals(someObjectThatIsHardToInstantiate).return(false);
Assert(equalityChecker.check(someObjectThatIsHardToInstantiate)).isFalse();
//verify that the mock was interacted with properly
Verify(someMockedObject).equals(someObjectThatIsHardToInstantiate).oneTime();
Is it appropriate to mock ComplicatedObject in this scenario?
© Programmers or respective owner