Search This Blog

Thursday, September 6, 2012

Litmus tests for using Service Stub over Mocked Object

During the RDNUG presentation tonight we talked about using  Service Stub or a Mock.  To provide a little more clarification on WHEN to use a Service Stub instead of a Mock I thought I'd share a little more here.

Service Stubs are essential when you have the following scenarios:
1) You need to interface with another system that isn't guaranteed to be available 100% of the time or can't be called (maybe production environment only).
2) The interface isn't ready yet.  Stubs are a great way to keep the project and delivery on schedule when you have external dependencies (e.g., services provided by a third party).
3) You have other sub-systems in your system that need a pre-fab reliable implementation which is somewhat a repeat of 1 & 2.

Mocks are essential when you need a very fast implementation of an interface that reliably behaves an intended way in order for you to test and isolate a class or component.  As exemplified tonight, Mocks will consistently perform faster in unit tests.  Again, consider 100 unit tests in a Continuous Integration scenario where each call averages 2 seconds.  200 seconds doesn't sound like a lot of time but think about the value add of CI; fast feedback loop.  When you have change sets coming in quickly (one or two small changes each change set) you want those tests returning quickly.  As shown tonight, consistent sub-second test responses can be achieved using Mocks. 

To summarize, use stubs when you need to simulate an external system and use mocks when you need to isolate in order to test behaviors in a dependent class.

I'll point to one of all time greats and a well spoken computer scientist who has helped me learn more about my chosen passion.  See Fowler's post here entitled "Mocks aren't Stubs"  http://martinfowler.com/articles/mocksArentStubs.html

Extending ASP.NET MVC with Dependency Injection Containers

Thanks to the Richmond.NET Users group for so graciously welcoming me tonight. 

You can download the source code for the discussion tonight here:

https://dl.dropbox.com/u/67850614/MVC4DIExtensibility.zip

The solution uses nuget packages so you shouldn't find any broken references anywhere.  I'd really like feedback from those who attended tonight.  I'm thinking about honing the presentation down into a much deeper talk specifically about using Spring.NET and only targeting MVC 4.  My thought is to build out more detailed examples of things like exporting POCOs using Spring.NET's WebServices exporter and implementing REAL aspects like caching and logging.

Thoughts?