One problem a lot of people face is not being able to test static methods. I am curious as to what other people may be using but this is a technique that I have been using that is fairly effective. I have to point out that I first found this technique in Ayende's Rhino Commons.
To demonstrate this techinque for testing static methods assume we have a class named Assert that has two methods in it IsTrue and Fail. It might look like this:
To test this class we can create an instance class that is a mirror image of the original class without the static keyword:
Now we are able to extract an interface from the newly created class:
Next we add a private static getter to the original class that is resolved using Windsor Container and change the static methods to delegate to the private getter.
We can now introduce a class that we can inject a Inversion of Control container into, this allows us to mock out the container at test time:
We also need to replace the getter in the static class with our newly created IocContainer class:
Our test for the static Assert class now looks like this:
Remember Me
a@href@title, strike
Theme design by Jelle Druyts
Pick a theme: BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2 BlogXP business calmBlue Candid Blue dasBlog dasblogger DirectionalRedux Discreet Blog Blue Elegante essence Just Html MadsSimple Mobile Mono Movable Radio Blue Movable Radio Heat nautica022 orangeCream Portal Project84 Project84Grass Slate Sound Waves Tricoleur useit.com Voidclass2
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Steven Rockarts
E-mail