Testing Theories Part 2 - Unit Testing with Inversion of Control Pro's/Con's

Another in a series of posts that in no small part will serve as summaries/reminders to myself on various readings on the topic of automated code testing.  Perhaps others will find them useful though.  I can't take credit for any terribly creative conjectures in my conclusions/summaries.  They almost exclusively come from the writers in the referenced material.  I would strongly encourage reading the original source materials for far more details and a better understanding of the topics.
 
Testing Theories Part 1 - Classical vs Mock Testing
Testing Theories Part 2 - Unit Testing with Inversion of Control Pro's/Con's
Testing Theories Part 3 - IoC Containers (Castle Windsor, Spring.NET), AutoMocking Containers
Testing Theories Part 4 - TypeMock Isolator - The Dark Horse of Unit Testing?
 
Inversion of Control (IoC) is often achieved by Dependency Injection and programming to interfaces and can be facilitated using IoC containers such as Castle Windsor or Spring.
 
Pro
  1. Improves modularity and extensibility through interface based programming - it's easier to add/swap out implementations in the future and mock frameworks are well suited to mocking interfaces
  2. Reduces coupling and dependency through isolation and decoupling into layers - layers can be replaced in the future or if that is not needed, they still gain the benefit of individual development and testing brought about by the reduction in dependencies
  3. Promotes Single Responsibility Principle - easier to prevent unexpected breakage from unrelated functionality in the same class
  4. All of the above facilitate creating fine grained unit tests that test only one thing and run quickly, especially using Mocks.
  5. Explictly defining your external dependencies in constructors makes them clear to users of the class serving to improve implicit documentation
Note: #1-#3 above result from a solid implementation of IoC principles.  Nevertheless, aside from the facets of isolation that result strictly from dependency injection, the rest of the principles mentioned in #1 - #3 above are not specific to inversion of control.  This is an important distinction as often arguments FOR IoC are just arguments for some of those good, base programming principles and IoC is not required for their implementation
 
Con
  • Leads to a lot of classes generated for interfaces and factories on objects that are not required for production code and many objects are perhaps known to be simple and very unlikely to change.  Violates YAGNI.
  • Tends to deviate from some facets of encapsulation given that dependencies which can arguably be considered to be implementation details are made external and must be known to be passed in/set.
  • Testability via IoC doesn't deal well with static methods or singletons.  While there may be certain good reasons for avoiding these at times, at others they are useful and do serve a purpose, but you have no choice
  • Similarly, while programming to interfaces is often useful and recommended, there are times when inheritance lends itself more easily to the design and IoC becomes kludgy to implement when dealing with a dependency that can consist of any of a inheritance chain of classes. [Need example for this - not as clear in my mind as when I had thought of this a while back]
  • Difficult to retrofit legacy code to use IoC
 
 
--- 
Resources 
More Debate: http://kohari.org/2007/08/15/defending-dependency-injection/

Pro

http://msdn2.microsoft.com/en-us/library/aa973811.aspx
http://crazybob.org/2007/06/introduction-to-guice-video-redux.html

http://64.233.169.104/search?q=cache:8Mb5G10FvbAJ:kohari.org/2007/08/15/defending-dependency-injection/trackback/+http://kohari.org/2007/08/15/defending-dependency-injection&hl=en&ct=clnk&cd=1&gl=us
http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx
http://blogs.msdn.com/ploeh/archive/2007/05/30/ReasonsForIsolation.aspx
http://www.ayende.com/Blog/archive/2007/08/21/Dependency-Injection-Applicability-Benefits-and-Mocking.aspx
http://ayende.com/Blog/archive/2007/08/18/Dependency-Injection-IAmDonQuixote.aspx
http://www.ayende.com/Blog/archive/2007/08/18/Dependency-Injection-More-than-a-testing-seam.aspx
http://www.ayende.com/Blog/archive/2007/08/23/Dependency-Injection-in-a-Dynamic-Environment.aspx
http://igloocoder.com/archive/2007/08/21/1286.aspx
http://igloocoder.com/archive/2007/07/25/1277.aspx - refactoring with ndepend as a guide
http://www.ayende.com/Blog/archive/2006/09/19/7262.aspx
http://www.netobjectives.com/streamzines/MockObjectsAndMockTurtles/player.html
http://www.ayende.com/Blog/archive/2006/07/02/7436.aspx - mocking System.Data ADO is a pain!  Easier to ignore the DB altogether
http://andersnoras.com/blogs/anoras/archive/2007/03/04/balancing-maintainability-readability-and-testability.aspx
http://weblogs.asp.net/rosherove/archive/2007/03/02/testable-designs-round-2-tooling-design-smells-and-bad-analogies.aspx
http://ayende.com/Blog/archive/2007/03/04/new-post.aspx
http://ayende.com/Blog/archive/2007/03/05/Great-Code-and-Average-Programmers.aspx
 
Share this post: Email it! | bookmark it! | digg it! | reddit!


Published Monday, February 18, 2008 2:04 PM by alecl
Filed under