expect-run-verify… Goodbye!

Expect-run-verify is the most common pattern in mock frameworks. (update: I called it record-replay-verify before – my bad – expect-run-verify is what I really meant).

What’s wrong with it?

  1. Codebase suffers. Test methods become noisy. I have to expect/record some interactions not because I want to test them but because mocks complain if I don’t do it.
  2. Doesn’t give me a natural test-drive. I prefer when expectations become assertions and appear after execution – it’s very intuitive.
  3. Tests are fragile. Test driving new functionality that requires additional interaction often breaks all tests. Thanks to the aggressive nature of expect-run-verify.
  4. Could give better failures. When expectation becomes assertion, failure shows exact line of code. No more deciphering exception messages – just click on first element on stack trace.
  5. Could read better. Separation of stubbing and expectations reads really nicely.

Today I discovered MoQ – little mocking library for .NET. When I read how MoQ was invented I see many similarities with my own thinking process that pushed me to write Mockito.

MoQ uses C# 3.0 sugar like lambda expressions. Mockito is a java creature. Both don’t like record/playback model. Mockito goes even further and abandons expect-run-verify.

dirtyharry

Expect-run-verify…

Goodbye!

Let me enjoy most natural test-drive: as close to state based testing as possible. Expectations in Mockito are like assertions – good old friends of state based testing.

Java TDDer have several options these days:

  1. Using hand crafted stubs/mocks: too much boilerplate, too many lines of code, chaos of stubs.
  2. Using existing mock libraries: less some obscure exceptions it’s all expect-run-verify stuff.
  3. Mockito: just try it and tell me what you don’t like about it :)

I know that many test-infected java people on this planet don’t use mock frameworks. They code mocks/stubs from scratch because they find mock libraries too annoying. Mockito offers simplicity and flexibility of hand crafted mocks and yet eliminates so much boilerplate code. I encourage you to try it out.

About these ads

17 Responses to expect-run-verify… Goodbye!

  1. [...] Faber nous présente dans expect-run-verify… Goodbye! les raisons qui l’ont poussé à créer un nouveau framework de mocks – Mockito – en partant [...]

    • Raynaldy says:

      Why couldn’t you just use mocks where you can, and shims where you can’t use mocks anmryoe ? seems like two complementary tools to me !Basically what you are saying here is Mocks are better than Type Isolations , which I agree with, but in some cases, type isolation is a necessary evil, and as there are no proper Free Type Isolation frameworks out there (I’m Thinking Type Isolator, and Telerik JustMock pro), I think it is a pretty good thing Microsoft did this. Reinventing the wheel with a Moq alternative would just be pointless As for your refactoring concerns are .. well.. concerned, I haven’t tried the thing for myself, so I guess I’ll have too see it with my own eyes to see how nice it plays with Resharper just my two cents

  2. James says:

    It sounds like you are thinking about objects in the wrong way and that’s why you are getting tied up in knots by expectations.

    You should not be thinking about “expect/run/verify” or the internal data stored in your objects.

    You should be thinking about the communication between your objects and describing those messaging patterns.

    Mockito can’t do that. For example, how can you say that in one state a query method is stubbed to return value A, then a single call to a command method puts the object into a state in which the query method will return value B.

    Abstract state machines one of the fundamental design techniques of OO programming, the stuff they teach in the first year programming courses at university. But Mockito can’t work with it. It’s only barely useful for OO programming.

    • Ljubinka says:

      The Nazis werent Ashkenazis maybe some not all toughh im sure there were Jewish nazis too .My question to you is Hitler was half Jewish and i have read mein kampf , ive had a theory for along time now its totally based on a hypothosis THeodore herzl started the zionist movent in 1890 or so the plan was to regain isreal at all costs they failed in world war 1 or at least suceeded at stiring the pot in germany for stage two ww2 ive often wondered if hitler was a useful idiot or a knowing idiot in herzls plans to regain isreal and obviously alot of other shit was gained from this too in my opinion hitler has done more to help the jews than to ever harm them look at how empowered the jews have been since ww2 mein kompf isnt and wasnt proof to me that hitler trully hated jews hell one of the jews greatest tricks is deciet it wouldnt be beyond them to right a book talking about how bad the jews are to actually further their goals even if it meant killing lesser jews to reach their goal.the jews use this tactic all the time divide and conquer ive often suspected hitlers intentions . before anyone gets their panties in a twist im just stating a hypothosis i have no factual info on this and no way to prove it ive just wondered about it is all.

    • The Nazis werent Ashkenazis maybe some not all though im sure there were Jewish nazis too .My question to you is Hitler was half Jewish and i have read mein kampf , ive had a theory for along time now its totally based on a hypothosis THeodore herzl started the zionist movent in 1890 or so the plan was to regain isreal at all costs they failed in world war 1 or at least suceeded at stiring the pot in germany for stage two ww2 ive often wondered if hitler was a useful idiot or a knowing idiot in herzls plans to regain isreal and obviously alot of other shit was gained from this too in my opinion hitler has done more to help the jews than to ever harm them look at how empowered the jews have been since ww2 mein kompf isnt and wasnt proof to me that hitler trully hated jews hell one of the jews greatest tricks is deciet it wouldnt be beyond them to right a book talking about how bad the jews are to actually further their goals even if it meant killing lesser jews to reach their goal.the jews use this tactic all the time divide and conquer ive often suspected hitlers intentions . before anyone gets their panties in a twist im just stating a hypothosis i have no factual info on this and no way to prove it ive just wondered about it is all.

    • I try not to take offense to things but I may find the efforts by the HRC to censure this show or impose their views of the world on others as a form of prior restraint.As a constitutional conservative, libertarian, cross dresser I tend to get peeved at anyone's attempt to stifle or shout down free speech. I understand that there is a difference between governmental sanctioned prior restraints and those that arise organically in the private sector but I think that bullying tactics by HRC are misplaced.I am a master/mistress of the remote control and in an instant I can zap off any program that I choose not to watch.I do not find comic drag to be offensive. I never thought to be offended by Tom Hanks and Peter Scolari when they did a similar show a generation ago. Robin Williams did not bug me in Mrs. Doubtfire.I am not a fan of productions that show men dressed as women for evil.What little I do know of 'Work It' the show seems fairly innocent.Two guys are unemployed for a year. One is single but the other has a wife and daughter to support. In order to feed and shelter his family he gets a job as a woman. It also seems that there is a theme that men in this situation learn to be more sensitive as a result of their living in a woman's world.I am not sure I will watch the show since I do not tend to watch sitcoms but from what I know I think this is a reasonable undertaking by ABC.PatPS: I am at least somewhat interested in seeing how the professional hair and wardrobe experts do their job in transforming the guys into presentable professional sales women.

    • You’ve managed a first class post

    • Jhon says:

      I hear Lily Allen was here last night!And Lisa wouldn’t pay 6 euro for RonaldoLOLThat cecrkad me up Franky do you remember me My all time favourite band is Modest Mouse

  3. szczepiq says:

    Mockito can’t do that. For example, how can you say that in one state…

    Are you complaining about the fact that Mockito mocks are stateless? Well, that’s the point! Mocks are lightweight, test-drive is a pleasure, test code is clean & simple which hopefully pushes the app code to be simple. We’re always open to suggestions, though. Show us an example from your code where you use mocking and mockito wouldn’t help you. So far, I have no problems in verifying behaviour using mockito.

    Abstract state machines one of the fundamental design techniques of OO programming, the stuff they teach in the first year programming courses at university. But Mockito can’t work with it…

    Simple and clean code is one of the fundamental techniques of building successful software, the stuff one learns in the first year of working as a software developer. Mockito shines in that respect.

  4. [...] end of the request, state is all that’s left). Some people argued for some kind of general state machine implementation in the mocking framework. I think this is utterly wrong. If you are writing java [...]

  5. Padcom says:

    Going over some talks on TechNet and Google Video I’ve discovered points of view about using mock frameworks that I’d never ever expect. For example some people seem to bring up the point that while using mocking frameworks is not inherently bad they do tend to hide the additional complexity that your interfaces might introduce. The reason for that was that if your interface is to big and you’re using Moq instead of creating an instance of it by yourself you will run into unnecessary dependencies and not even see it coming.
    Even though I understand those kind of arguments I still believe that both Moq and Mockito are great frameworks and you know what? I use both all the time :D

    Keep up the good work!

  6. и всё эе: мне понравилось..

  7. Jeff says:

    I really like Mockito but I am also writing C#. Do you have any plans to port Mockito to C# or do you know anyone else who is working on a port?

    Thanks

  8. szczepiq says:

    According to my knowledge (and you should never trust it too much!) most mocking tools for C# support spying style. That is verify-afterwards flavor. I remember Moq library was quite cool at the time.

  9. duhxuoyb3760 says:

    ostrato indipendenza presto . nel 1967 e nel 1974 , rispettivamente , divenne primo membro della direzione della camera di opporsi pubblicamente il presidente johnson sulla guerra del v
    [i][url=http://www.offertepiumini.com]moncler piumini outlet[/url][/i]
    convinzione , secondo canberra aiutanti , è che i buoni rapporti con così grande un vicino sono di estrema importanza e che il controllo indonesiana di timor est è un fatto di ruolo
    [url=http://www.stivalimilanovendita.com]stivali ugg[/url]
    a disoccupazione , inoltre, è sceso ai minimi livelli sostenuti dalla fine 1960s.as l’attuale espansione economica degli stati uniti entra nel suo ottavo anno , la fed sarà accertato
    [url=http://www.offertepiumini.com]www.offertepiumini.com[/url]
    e discutibile . c’è sempre qualche pericolo , in una tale impresa , di non riuscire a vedere la foresta per gli alberi.da tempo superato dalle onde d’urto di un teatro più esplicito ,
    [url=http://www.stivalimilanovendita.com]www.stivalimilanovendita.com[/url]

  10. Spectacular internet site. Numerous information here. I will be giving it to a few buddies ans as well expressing around delectable. And clearly, thanks for your perspire!

  11. Hey would you mind letting me know which web host you’re using?
    I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot quicker
    then most. Can you suggest a good hosting provider at a fair price?
    Thanks a lot, I appreciate it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 38 other followers

%d bloggers like this: