title: Play route/Controller just for unit test
body: <p>I have a method that takes a Play <code>Http.Context</code> and "does some stuff" with the session. I want to write a unit test for just that method. Specifically, I want to test that if a request comes in with certain headers my method works correctly. It seems like the easiest way to do that reliably is to create a <code>FakeApplication</code> and a <code>Controller</code> for my test. Then I'd use <code>Helpers.fakeRequest</code> to get a request and <code>Helpers.route</code> to route that request to my controller. The controller would call my method, set some variables, etc. and I could then assert success and such.</p>
  
  <p>Seems like a splendid plan but I can't figure out how to add a route to my controller in the <code>FakeApplication</code>. Note that this controller isn't really part of my app - it's just something I want to use for this one test. So I want to define it and construct in just this one unit test; I don't want to add it to my <code>conf/routes</code> file.</p>
  
  <p>Specifically, I want something like this:</p>
  
  <pre><code>// Maybe I can use GlobalSettings.onRouteRequest but the return type
  // is play.api.mvc.Handler which seems inaccessible from Java
  FakeApplication app = Helpers.fakeApplication(new MyGlobalSettings());
  Http.Request request = Helpers.fakeRequest().withCookies(...).withBody(...);
  Controller testContoller = new MyTestController();
  // This doesn't exist, but I want something like this
  app.addRoute("/foo", ctx -&gt; testController.method(ctx));
  running(app, () -&gt; {
      Helpers.route("/foo");
      assertThat(testContoller.itWorked()).isTrue();
  }
  </code></pre>
  
  <p>I'm running Play 2.2.3 and writing in Java, not Scala.</p>
  
  <p>I do realize I can construct an <code>Http.Context</code> directly and pass that to my method. However, this isn't my preferred approach for a few reasons:</p>
  
  <ul>
  <li>The <code>Http.Context</code> constructor takes the plain text of the session variables. I want to test that things work correctly when the request contains the encrypted session cookie.</li>
  <li>The <code>Http.Context</code> constructor is poorly documented and seems a bit off. For example, you can pass an <code>Http.Request</code> to the constructor, but you also pass the cookie data and session data. So what happens to the cookie/session data on the request? Does it get merged with the other data passed? Ignored?</li>
  <li>The <code>Http.Context</code> constructor is difficult to use from Java as it requires a <code>play.api.mvc.RequestHeader</code>, which can't be constructed in Java, and a <code>play.mvc.Http.Request</code> which can't be "usefully" constructed from Java (you can construct one, but without cookies, headers, etc. and <code>FakeRequest</code> can't be converted to an <code>Http.Request</code>).</li>
  <li>It feels more "black box" to send in a request and ensure things work rather than try to figure out how this particular version of Play converts my request it an <code>Http.Context</code> (e.g manually constructing a context seems more likely to break with new versions of play).</li>
  </ul>
  
  <p>Any ideas?</p>