Don’t Mock! – To Mimick is Better?

Do mimick – Don’t mock

This is the headline advice.

mimick a service - instead of mocking an object.

Why Mimicking is better than Mocking

Mimicking is better than mocking because you get

  1. higher productivity (less code) when you develop software
  2. deeper penetration – your tests penetrate further through service layers
  3. to say no to mocking frameworks – JMock and EasyMock work well but quickly become maintenance headaches
  4. to avoid coupling your tests to service implementations

We all know what mocking is. We write mock objects to safely test operations like sending e-mails, talking to databases and even taking a payment by credit card. Many use mocking frameworks like JMock and Easy Mock.

But what is mimicking (when it’s not at home)? And why is it better?

What is Mimicking

Mocking is code that you write – pretending to be a service. You write no code with mimicking and there is no need to pretend. Mimicking is a service while mocking is your code trying to be a service.

That is why the headline advice tells you to

mimick a service - instead of mocking an object.

Mimicking is already commonplace. And with Docker containers mimicking gets even easier.

Mimicking Already Happens

When you feel like mocking – ask yourself whether mimicking is an option. Mimicking already happens so let this list teach you the concept of mimicking.

We mimick when

  1. the credit card service provide gives us a test sandbox and some valid-ish card numbers
  2. we use an in-memory database like HSQL to help us test our persistence code
  3. we use curl to send web service requests to test our newly-built endpoints
  4. we send e-mails to an actual e-mail service configured not to forward them

Let’s visit an example where we mimick an e-mail service instead of mocking it.

Mimicking (not Mocking) An E-Mail Service

Service Penetration. If we write mock objects “pretending” to send e-mails we don’t get to test the connections nor the authentication nor many other facets. If we sent the subject in the emailBody and sent the body in the emailSubject our mock objects will subserviently give the thumbs up.

However we can mimick using a real e-mail service (or one configured on an accessible host). It will test the connection and authentication and can even display the test e-mail on a web page so that we can assert that all the attributes (subject, body) were correctly sent in the right places.

Service Providers Don’t Mind You Mimicking

Service providers understand the need for mimicking so they always provide a mimicking option. This includes cloud services, payment services, authentication services, persistence services, and the biggest giant of them all – web services.

We service end-points now come with a parallel end-point that allows your tests to penetrate deep into the service call chain and verify right up to the cliff edge.

Mimick – And You’ll Have the Last Laugh

Mimicking cuts away test code, penetrates deeper and talks directly to the service in question.

When you mock you probably don’t get the joke, when you mimick – you hear it from the horse’s mouth – and that’s no bad thing.

Once you mimick services in your unit, integration and system tests, it will be lights out and away you go!

Leave a Reply

Your email address will not be published. Required fields are marked *