Mocking

2 views
Skip to first unread message

earthwormgaz

unread,
Jun 24, 2008, 11:51:34 AM6/24/08
to Test Driven Development
Hello list,

Say I've got class A that I want to unit test, it makes use of classes
B and C, and they in turn make use of D which is in another namespace/
library/directory/package, which classes should I be looking to mock
out and add test hooks to?

Nick Parker

unread,
Jun 24, 2008, 1:05:53 PM6/24/08
to Test-Driven...@googlegroups.com
earthworkgaz,

I would suggest mocking B and C, so if A defines a concrete impl, swap out B and C in favor of an interface as arguments for A.  A should be concerned with the calls it makes to B and C, but not what they do (that's the implementation).

Nick Parker
www.developernotes.com

Tim Gifford

unread,
Jun 25, 2008, 5:21:53 PM6/25/08
to Test-Driven...@googlegroups.com
Agreed. However, I had to read that a few times to understand my ABC's.

So A should have a constructor that takes in the interfaces for B and C. I'm calling those IB and IC respectively.

Example:

public A ( IB b, IC c)
{
 this.b = b;
 this.c = c
}

Then you pass in mocked implementation of IB and IC.

Philippe

unread,
Jun 26, 2008, 7:37:05 AM6/26/08
to Test Driven Development
Hello,

Perhaps you should first ask yourself why you are looking to mock
them ?
Do you need to mock them or do you want to mock them ?

if you need to mock them, then the reason might also be the answer: if
B and C are not available to you yet, you have to mock them. If D
accesses a database, then you could only mock D.

Michael Feathers listed the reasons to mock an object :
# It talks to the database
# It communicates across the network
# It touches the file system
# It can't run at the same time as any of your other unit tests
# You have to do special things to your environment (such as editing
config files) to run it.

http://www.artima.com/weblogs/viewpost.jsp?thread=126923

Otherwise, you can unit test B&C and D thru A, that's fine.

HTH,
Philippe
http://pantras.free.fr
Reply all
Reply to author
Forward
0 new messages