title: Setting HttpContext.Current.Application key in unit tests
body: <p>I have a method which internally uses the static <code>HttpContext.Current.Application</code> to store a key-value pair and retrieve it throughout the application afterwards.</p>
  
  <p>Now, what I want to do is to unit test that method. What I currently have is this:</p>
  
  <pre><code>private const string Foo = "foobar";
  
  [TestInitialize]
  public void InitializeContext()
  {
      HttpContext.Current = new HttpContext(new HttpRequest(null, "http://tempuri.org", null), new HttpResponse(null));
      HttpContext.Current.Application["fooKey"] = Foo;
  }
  
  [TestCleanup]
  public void DisposeContext()
  {
      HttpContext.Current = null;
  }
  </code></pre>
  
  <p>All good, but when I try to cast that value in the <code>TestMethod</code>, the value is null.</p>
  
  <pre><code>var fooValue = (string)HttpContext.Current.Application["fooKey"];
  </code></pre>
  
  <p>Obviously, the value is null in the method which I'm testing as well.</p>
  
  <p>What I tried so far is to mock the context - didn't work as well. Then I saw the fake HTTP context solution on SO, still doesn't work. I even tried accepting <code>HttpApplicationStateBase</code>/ <code>HttpApplicationStateWrapper</code> and working through them, but it becomes a mess and I feel that there's a lot simpler solution than that.</p>