I’ve got a method which I’ve added unit tests for in a C#/ASP.Net web project. The method has been modified by someone else to include a call to a static method on a class which wraps an HttpContext (to add some session state), but during testing I don’t have an HttpContext, so this throws a null reference exception. Any ideas how to get round this problem? I don’t want to make too many changes to production code if I can help it.
Method under test:
public int MethodUnderTest()
{
...
// Added line which breaks the tests
StaticClass.ClearSessionState();
}
In StaticClass:
public void ClearSessionState()
{
HttpContext.Current.Session["VariableName"] = null;
}
This throws a NullReferenceException because HttpContext.Current is null during testing.
In the end I could just remove the call to
StaticClass.ClearSessionState(), but thanks for all the answers. Useful stuff.