Beyond Stubs: Why We Need Interaction Testing

Consider the moment when the barber holds up the mirror so you can examine the cut of your newly trimmed neckline. Have you ever been slightly surprised by the realisation that, firstly, the back of your head actually exists and, secondly, how unfamiliar you are with what it looks like? Interaction testing (Mocking) is somewhat similar. You’ve probably been testing this stuff for years but have you ever really peered round to examine the far side of a class and how it interacts with its collaborators?

When you start to practice TDD you often discover the need for stubs as you try to isolate the functionality you’re trying to test (as discussed in the previous article – here). However stubs alone leave the Test First developer with a problem: Some classes simply do not expose a change in state, making them hard to test using state based assertions as there is nothing to assert on.

Most developers will solve this problem by adding some basic functionality to a stub object so as to expose evidence of the ‘interactions’ of the object under test to make up for the lack of a state change. For example, if we had an object with a callback we might provide a stubbed implementation in the test that has a a boolean ‘callbackMade’ which is asserted on in the test. The development of such active stubs is the first step on the road to Interaction Testing.

But Interaction testing is much more than stubs with a little functionality added; it’s a change in the testing paradigm. Testing becomes about the behaviour of objects rather than  a function of the system. The tested landscape morphs to include state and interactions so that no matter how an object decides to behave we can make critical assertions. This encourages the testing of objects with well defined responsibilities, rather than functionality of the system. However to see how Interaction Testing changes the way we test, first we must understand why we need Interaction testing at all.

Unit testing becomes about the behaviour of objects rather than  the function of the system.

Consider an extension to the call stack model, presented in the last article (here). The figure below evolves this model by adding branching of the call stack, this being a better approximation of reality. Small branches, such as the one highlighted at the top, are naturally encapsulated making them easy to test with simple, state based tests.branched stack

However classes whose primary concern is to aide the forwarding of the call stack (rather than exposing a change in state) present a tougher problem. For example the lower circled interaction in the figure below. Such objects are defined solely by how they interact with collaborating (composed) objects.

Examples of such types of classes include Proxies, Marshallers and Composites, to name but a few, none of which change state as a result of their behaviour.

Lets consider the example of a composite object which forwards invocations to a set of delegates that share the same interface. To test such a Composite Object assertions should be made on the calls it makes to the objects being composed (all the red circles on the figure), so that we can ensure that this contract, of forwarding calls, is honoured.

mock assertion

This could be done with a stub that tracks invocations. The success of the test is then based on the stub’s invocation counter being incremented by each forwarded call the composite makes.

This moves us towards a different paradigm; Interaction Testing, where tests assert how an object interacts with its collaborators rather than how it changes state.

Mocking frameworks assert that objects interact with their collaborators in an expected way. For example, extending the composite example used above, we assert that the composite successfully validated all delegates. Mocking frameworks however provide a better approach to interaction testing than manually crafting ‘active’ stub objects as they are syntactically more succinct and provide a wealth of rich features for testing different types of interaction. So the use of mocking frameworks provides the developer with two important functions that shape the way they develop:

1) A succinct tool that allows them to demarcate the area under test with stubs.

2) A method for asserting on the interactions that a class makes with its collaborators when such assertions are important to the test.tdd

When we practice TDD with mocking frameworks both of these tools have a significant affect on the process we follow. Firstly Mockist developers only need to concentrate on developing one class at a time. Classes are developed in isolation with mocked interfaces providing detail of the roles collaborators must provide. This in turn leads to a rapid loop –  writing a test, writing the implementation – teasing out collaborating roles as mocks. Once the class is completed focus is turned to one of the previously defined collaborating roles and the cycle is repeated. This encourages the prescription of specific roles that collaborators must perform rather than simply defining entities. The process is akin to Design by Contract and some refer to this as Need Driven Development.

Secondly we can test any class, whether it results in a state change or interaction. Both tools are immediately available to us and this makes for better, clearer testing. However mocking has its own unique problems, the most sited of which is the increased coupling between test and source. For example Martin Fowler’s article on the subject of Mock based testing states:

“I’ve always been an old fashioned classic TDDer and thus far I don’t see any reason to change. I don’t see any compelling benefits for mockist TDD, and am concerned about the consequences of coupling tests to implementation.”

Martin is alluding to the fact that Interaction Testing increases the length of metaphorical rope available for a team to collectively hang themselves. This is often reported as test code that increases the cost of maintenance, making refactoring harder as changes in the internal workings of a class, in a way that affects the interaction with its collaborators, will cause tests to break. This is demonstrated in the figure below.

The retort to this is simple. If the interaction is truely significant then it should be asserted on. Why should a change in state on the front interface be more important than a change in interaction on the rear one?

In addition the increased coupling is rarely the root cause of the horror stories associated with mock driven development. The reported symptom of excessive coupling between test and source can more often be attributed to a lack of experience in writing mock-based tests than a problem that is intrinsic to the method. The telltale sign of such issues are conditions under test being obfuscated by mocked interactions that have no relevance, which in turn arises from a poor use of stubbing.

“Mocked tests amplify poor design in the code they are testing.”

There are many causes for problems with Mock driven development; under use of stubs and attempting to retrofit mocking into code that was not designed to facilitate it being the most frequent that I see. The key point however is that, whilst mocks do increase the coupling between test and source, the increase is negligible if the mocks and the source they test is well designed. Mocking brings with it a change in process and a change in thinking. This is not for everyone, and it is certainly not the simplest way to develop, but this change in thought process is fundamental and, if you are serious about your testing, the benefits may be irrefutable.

Posted on February 18th, 2010 in TDD


  1. Kid-on-the-yard May 15th, 2025
    15:00 GMT

    Hey, I think your site might be having browser compatibility issues.
    When I look at your blog in Opera, it looks fine but
    when opening in Internet Explorer, it has some overlapping.

    I just wanted to give you a quick heads up!
    Other then that, terrific blog!

  2. slot gacor malam ini May 18th, 2025
    18:23 GMT

    I know this website provides quality depending posts
    and other information, is there any other site which offers such things in quality?

  3. Ev88 June 19th, 2025
    0:31 GMT

    Valuable information. Lucky me I discovered your website by chance, and
    I’m shocked why this coincidence did not came about in advance!

    I bookmarked it.

  4. WSCC model examples June 26th, 2025
    2:40 GMT

    I do believe all the ideas you have offered to your post.
    They’re very convincing and will certainly work.
    Still, the posts are very quick for newbies.
    May just you please extend them a bit from subsequent time?
    Thanks for the post.

  5. classifieds.ocala-news.com July 9th, 2025
    19:33 GMT

    That is content I’d share with out hesitation.

  6. trusted ira providers in gold July 9th, 2025
    21:22 GMT

    I’ve read a lot on this topic and your take felt
    the most balanced.

  7. gold and silver ira companies July 10th, 2025
    6:35 GMT

    Thanks for taking the time to jot down this.
    I realized one thing new and will certainly be making use of it.

  8. 23Win July 12th, 2025
    10:40 GMT

    Good day I am so thrilled I found your website, I really found you by accident, while I was searching on Aol for something
    else, Anyways I am here now and would just like to say cheers for a
    remarkable post and a all round interesting blog (I also love the theme/design),
    I don’t have time to browse it all at the moment but I
    have book-marked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the great jo.

  9. 789f July 12th, 2025
    10:47 GMT

    Hi there, all the time i used to check webpage
    posts here in the early hours in the dawn, because i love to gain knowledge of more and
    more.

  10. 79king com July 13th, 2025
    5:01 GMT

    Hi there! I could have sworn I’ve been to this site before but after browsing through some of the post I realized it’s new
    to me. Nonetheless, I’m definitely delighted I found it and I’ll be bookmarking and checking
    back often!

  11. https://99win.wang/ July 19th, 2025
    15:35 GMT

    Hello all, here every person is sharing such knowledge, so it’s good to read this
    website, and I used to go to see this weblog every day.

  12. Sodo88com July 19th, 2025
    20:16 GMT

    You’re so cool! I do not suppose I have read anything like this
    before. So nice to find another person with a few genuine thoughts
    on this subject. Seriously.. many thanks for starting this up.
    This site is something that is needed on the internet, someone with
    a little originality!

  13. 79king rodeo July 20th, 2025
    16:36 GMT

    My spouse and I stumbled over here from a different web address and thought I should check things out.
    I like what I see so i am just following you. Look forward
    to looking over your web page for a second time.

  14. https://79king.luxe/ July 20th, 2025
    18:40 GMT

    If some one wants expert view about running a blog then i advise him/her to pay a visit this website,
    Keep up the good job.

  15. MM99 July 21st, 2025
    4:14 GMT

    What’s up, I would like to subscribe for this website to
    take hottest updates, therefore where can i do it please assist.

  16. 23win23.skin July 21st, 2025
    6:26 GMT

    Hi there! This is kind of off topic but I need some guidance from an established blog.
    Is it tough to set up your own blog? I’m not very techincal but I
    can figure things out pretty quick. I’m thinking about setting up my own but I’m not sure where to start.
    Do you have any ideas or suggestions? Cheers

  17. bán dữ liệu người dùng July 21st, 2025
    6:48 GMT

    Thanks for another excellent post. Where else could anybody get that type
    of information in such an ideal way of writing?
    I have a presentation subsequent week, and I’m on the search for
    such info.

  18. best gold ira providers July 21st, 2025
    21:28 GMT

    Really like the way you broke this down. It made it feel
    manageable.

  19. 333win vin July 22nd, 2025
    19:35 GMT

    Your mode of explaining all in this post is
    in fact pleasant, every one be able to without difficulty be aware of it, Thanks a lot.

  20. 97win97 com July 23rd, 2025
    6:31 GMT

    Hi there just wanted to give you a quick heads up. The words in your content seem to be
    running off the screen in Safari. I’m not sure if this is a
    format issue or something to do with web browser
    compatibility but I figured I’d post to let you know. The design look
    great though! Hope you get the problem solved soon. Many thanks

  21. Floyd July 25th, 2025
    3:09 GMT

    Absolutely love the idea of a glider in the nursery.

    It’s such a game changer!
    |
    What a helpful post—I was looking for something like this.
    Thanks for writing this!
    |
    We often forget mum’s back!, and this rocker looks supportive.

    |
    I just added one to my nursery, and truthfully it’s been a lifesaver.

    |
    Never realized how important a swiveling chair could be
    until I became a mum.
    |
    Such a well-thought-out idea. Rockers are perfect for those sleepy nights.

    |
    I’ve been looking at nursery chairs, and this blog made my decision easier.

    |
    As a new mum, I {need all the comfort I can get|love hearing

  22. Michel July 25th, 2025
    3:42 GMT

    Hi would you mind letting me know which hosting company you’re using?

    I’ve loaded your blog in 3 completely different internet browsers and I
    must say this blog loads a lot quicker then most. Can you recommend a good hosting provider at a reasonable
    price? Thanks a lot, I appreciate it!

  23. Gladys August 1st, 2025
    1:50 GMT

    I was suggested this blog through my cousin. I am
    no longer sure whether or not this publish is written by means of him as no one else recognize such particular approximately my trouble.
    You’re amazing! Thanks!

  24. Magnolia August 11th, 2025
    14:15 GMT

    What i don’t realize is in fact how you’re now not really much more
    neatly-favored than you may be now. You are very intelligent.

    You know thus significantly in the case of this subject, produced me in my
    opinion consider it from a lot of various angles. Its like women and
    men don’t seem to be fascinated unless it’s something to accomplish with Lady gaga!
    Your own stuffs great. At all times handle it up!

  25. mbi123 August 12th, 2025
    17:12 GMT

    Ahaa, its fastidious discussion on the topic of
    this piece of writing here at this web site, I have read all that, so now me also commenting here.

  26. www.tumblr.com August 13th, 2025
    23:46 GMT

    Very nice post. I absolutely appreciate this site.
    Continue the good work!

  27. bespoke reception furniture london August 14th, 2025
    8:24 GMT

    great put up, very informative. I’m wondering why the other experts
    of this sector do not notice this. You must proceed your writing.
    I am confident, you’ve a huge readers’ base already!

  28. Self Checkout Pos System August 19th, 2025
    17:25 GMT

    Brilliant POS is the foremost distributor of sales systems for
    pubs and restaurants in the USA. Their advanced technology is developed to fulfill the unique requirements of hospitality industry.
    The systems offer seamless order management and speedy transaction processing allowing staff to assist customers effectively.
    With intuitive interfaces Brilliant POS makes sure that workers
    can effortlessly navigate the system, lowering training time
    and errors. The software integrates with inventory management,
    providing real time tracking of stock levels, which aids prevent
    shortages and overstocking. Brilliant POS delivers in-depth sales reports, allowing owners to make informed decisions that drive profitability.
    Their customer support is superb, offering quick assistance whenever needed, which is crucial for demanding establishments.
    The hardware is robust and reliable, constructed to handle the fast paced environment of bars and restaurants.

    Brilliant POS also supports multiple payment options, including contactless payments, ensuring the checkout process smooth
    for customers. Security features secure sensitive data, ensuring compliance with industry standards.

    The system is scalable, appropriate for small independent bars as
    well as large restaurant chains. Brilliant POS constantly upgrades their software, incorporating the latest technology trends to help keep businesses ahead.
    Choosing Brilliant POS means investing in a comprehensive solution that enhances operational efficiency and elevates customer experience.

    Their standing for quality, innovation, and exceptional service
    renders them the top POS systems provider for bars
    and restaurants all over the United States.

  29. luxury interior designers london uk August 20th, 2025
    2:56 GMT

    Howdy! This post could not be written any better!
    Looking through this article reminds me of my previous roommate!
    He always kept preaching about this. I will send this information to him.

    Pretty sure he’s going to have a great read. Many thanks for sharing!

  30. Renewable Energy Solar Panels August 24th, 2025
    21:32 GMT

    Lumina Solar serves the Philomont, VA area with outstanding expertise in solar panel installation, making them a prime selection for
    renewable energy solutions in Virginia. Philomont, situated around 39.11°N latitude and 77.66°W longitude,
    is part of Loudoun County, known for its prosperous community and rising need for sustainable energy.
    With a median household income above $140,000, many residents in Philomont seek
    efficient power options, a need Lumina Solar skillfully meets.
    The region experiences an average of 205 sunny days per year,
    providing ideal conditions for solar energy generation, which Lumina Solar utilizes to maximize system efficiency for their customers.
    Key points of interest nearby, such as the Sky Meadows
    State Park, appeal to green-minded travelers, further highlighting
    community values aligned with green energy. Lumina Solar’s skilled team is familiar with local
    regulations and utility incentives specific to Virginia, ensuring smooth installation and permitting processes.
    By serving the Philomont area, Lumina Solar supports a community that values sustainability and energy independence, using their technical proficiency and regional knowledge to deliver
    tailored solar solutions that meet the unique
    demands of this demographic and geographic landscape.

  31. https://wavedream.wiki/ August 27th, 2025
    18:43 GMT

    I know this web site provides quality based articles
    or reviews and extra information, is there any other site which provides such stuff in quality?

Have your say

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>




Safari hates me
IMPORTANT! To be able to proceed, you need to solve the following simple problem (so we know that you are a human) :-)

Add the numbers ( 9 + 5 ) and SUBTRACT two ?
Please leave these two fields as-is:

Talks (View on YouTube)