logo
  • Jobs
  • About Me
  • Contact
  • Home
« A Test Runner for mbUnit
Google AdSense for Feeds »

Provider Pattern and Unit Testing

Posted October 18th, 2005 by Matt Berther

I’ve recently wrapped up a project using .NET 2.0 and this project manifested the need to have certain pluggable pieces. For these pieces, I chose to go along with the rest of the .NET 2.0 framework and implemented this using the Provider design pattern.

Following the pattern strictly involves deriving from ProviderBase and then using the ProvidersHelper class to instantiate all the classes from the configuration section.

The ProviderBase class has an Initialize method that gets called by the ProvidersHelper.InstantiateProviders method. The other thing is that you should be creating static methods that delegate to the underlying (previously instantiated) provider.

Does the Provider pattern lend itself well to unit testing? Let’s look at a set of unit testing rules suggested by Michael Feathers. His last rule is that a test is not a test if you have to do special things to your environment (such as editing config files) to run it.

I’m leaning towards thinking that the extra layer (the class that has all the static methods) is not worth the headache. Would it be better if there was a base class or an interface defined, and then you could test the implementation directly?

Sure, you could test the implementation directly now, but to get the information into it now, you’d have to perform additional setup and pass that off to the Initialize method. The principles of dependency inversion state that an object should be given its dependencies, rather than asking for them. Given this, it makes sense that these items go into the constructor of the class.

As our company becomes more familiar with the principles of unit testing, I fear that we will fall into this situation more and more. Given what I know now, I will certainly advise against the Provider pattern in favor of an interface/factory class approach, which will also facilitate integration with a dependecy injection container (such as Castle Windsor).

Are you unit testing providers? Are your experiences similar? Id love to hear about it.

2 Comments

This entry was posted on Tuesday, October 18th, 2005 at 7:53 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Milan Negovan
October 19th, 2005

You made a very good point about having to tweak configs to unit test with providers. I’m not testing with providers yet, but I do prefer interfaces/factories.

Ironically, you can implement “provider interfaces” and thus write custom providers, but they are so messy that they are hardly worth the trouble.

Adam
January 7th, 2009

Yeah, I’m struggling to figure out how to test with dependency injection when we have a provider pattern (sort of) already implemented enterprise wide. I don’t necessarily want to TEST the provider, but I want to test the DAL classes which USE a provider. Unfortunately, I have found no way to elegantly inject that dependency of the provider, since as you point out, the .NET Provider model used a base class and uses a configuration file to determine the properties of the concrete provider. I don’t want to change the overall architecture, since its implementation is so entrenched. It would take a major project charter to refactor the implementation for testability.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>
-->

flag
Favorite Charity
wounded warrior project
Search
Social
  • mattberther on twitter
  • mattberther on linkedin
Syndication
Archives
  • January 2010
  • September 2009
  • July 2009
  • June 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • September 2008
  • August 2008
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
mattberther.com © 2003 - 2010