This post originated from an RSS feed registered with .NET Buzz
by Jerrel Blankenship.
Original Post: Removing Static Method Dependency via Adapters
Feed Title: The Agile .NET Developer
Feed URL: http://www.jerrelblankenship.com/feeds/posts/default?alt=rss
Feed Description: A Blog about Agile and .Net and anything else related to software development
There may come a time when you need to use static method calls in a method that you are creating or modifying. That is not a bad thing, per se, but the problem will arise once you try to unit test that method.
The idea behind a unit test is to test only the functionality in the method. To truly test only the functionality in the method however, you need to remove all of the outside dependencies. When you are testing a method that has a static method call, you can’t remove that dependency because there is no way to stub or mock a static method call. There is a work around for this dilemma that will allow you to mock out the static method call and truly unit test your method.
An adapter class is a class that allows you to wrap an instance class around the static class. The adapter class is just a pass through to the static class method, but once you have the adapter class, that class can implement an interface that you could mock out when unit testing.
Below, I will show you the original method with the static call and a step by step process to remove that dependency from your test.
Original method with static call
publicbool LoadDB()
{
bool result = true;
// static method call
result = pdDataBase.SetupDatabase();
if (result && !_adapter.RefDataLoaded)
{
// static method call
pdDataBase.LoadRefData();
}
return result;
}
What you see is a method that when we write the unit tests for we will be unable to remove the highlighted dependency. Using the adapter class process, we can remove these dependencies.
First thing is to create an adapter class for the static class that contains the method you need to remove. The adapter class is named the same as the static class, except has “Adapter” tacked on the end. You can give the class a name like “StaticClassNameAdapter.vb”.
Inside that class file create an interface called “IStaticClassNameAdapter”. You could also create a separate interface class file as well. This interface is how you will communicate with the adapter class and by proxy to the static class. By communicating through the interface, you have the ability to mock out that interface so that you can return a known object when unit testing. Done this way, you will have a known object without having to touch web services, databases, etc.
The interface will contain a method definition that mirrors the static method call. When the adapter class implements the interface, that method can call the static method. This seems like a lot of work to handle a static method, but this can give you a lot of flexibility moving forward as well as a way to test methods now.
Once we have the adapter class and the interface, you will make two changes to the class that contains the method that originally called the static method.
Add a new parameter to the constructor of the class that is of type of the interface you created for the adapter.
Change the method call to call the adapter instead of the static class method.
publicbool LoadDB()
{
bool result = true;
result = _adapter.SetupDatabase();
if (result && !_adapter.RefDataLoaded)
{
_adapter.LoadRefData();
}
return result;
}
Using adapter interfaces and classes around static method does not change the underlying functionality of the method. All this does is gives us the ability to remove a dependency from the method when we want to unit test that method.
What are some other options to remove static method call dependencies from methods for unit testing purposes?
Here are some additional links if you want further research: