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.

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 ( 10 + 8 ) and SUBTRACT two ?
Please leave these two fields as-is:

Talks (View on YouTube)