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?

Wednesday, May 30, 2012

Web Testing with Microsoft Visual Studio 2010 Ultimate: Context Parameters

This represents the first of a number of blog posts I hope to write over the next few weeks.  As my colleagues and I continue to deep-dive into the web testing tools provided by Microsoft Visual Studio 2010 Ultimate, I hope to share what we learn with you.

Today, my colleagues and I discussed the concept of test organization strategy.  In short, this translates to building reusable "chunks" of tests.  A great example of a reusable test is "Logon to site".  These is absolutely no reason to duplicate this effort across all tests you record.  Instead, record logon, parameterize the username and password and save it as a ".webtest" file.  The same approach applies to logging off a site.

The primary reason to do this is to promote reuse of webtests across your testing repository.  I'm not a test engineer so I have to give credit where credit is due.  My good friends and colleagues Guru Shyam Mony and Azmathulla Mohammed drilled this into my head and it simply makes sense from a test organization perspective.

Once we start going down this path, ultimately we'll want to create composite web tests.  Composite Web Tests a simply web tests that call other web tests.  Typically a single composite web test will represent a unit of work to be tested, or in test engineer terms a test scenario. 

Once we start creating composite tests, there will almost always be situations where we want to pass context parameters from the composite test to its child test.  I was surprised to find tonight how easy this actually is.

Step 1:  Create a context parameter in the "child test" and give it a default value that will allow the "child test" to pass.
Step 2:  In the composite test, create a parameter with the same name as that in the "child test".  Choose your method of setting the value for the context parameter.
Step 3:   Run the composite test and create some form of validation that ensures the value is passed from the composite test.

What you'll find is the composite test will in effect override the "default" value of the context parameter in the web test it calls and supply its own value. 

A great example of where this type of behavior may be desirable is as follows:

1) User logs in
2) Data source drives a set of dates and each date is supplied to a context parameter in the composite test.
3) Composite test calls a data entry web test and needs the date as a value.
4) Composite test calls a "review entries" web test and needs the date as a value.

We can essentially control the composite test and its subordinates with a single data source and a context parameter.

Friday, March 9, 2012

Rethinking Web Services

A recent tweet from Dino Esposito mentioned agreement with a blog here.  Web services as we know them historically are not the be-all end-all for every solution and as technology folks we need to be thinking about applying the right-sized solution for the problems we are faced with.

In fact, in a recent integration effort we opted for what I'd refer to as 'UltraLite' web services built on top of ASP.NET MVC.  Our target audience for integration is, after all, well versed in our payload format (JSON) and with added support for mapping JSON into view models, we're still working in a known world in our controller actions.  With added testability of controller actions, we also have the capability of supporting end-to-end testing with isolation and our integration testers can use tools they are comfortable with to do integration testing (yes, they can use a web page or a web client or whatever).

Add to the above, rich data annotations, model state validation and you have basic validation support as well.