title: Testing controller with fake session
body: <p><strong>I want to write test for my controller:</strong></p>
  
  <pre><code>Result changeAction = callAction(controllers.routes.ref.Users.changePassword());
  assertThat(status(changeAction)).isEqualTo(OK);
  </code></pre>
  
  <p>I have a http status code - 300.</p>
  
  <p>That's right it is redirect because I have a class named Secured</p>
  
  <pre><code>package controllers;
  
  import play.mvc.*;
  import play.mvc.Http.*;
  
  public class Secured extends Security.Authenticator {
  
      @Override
      public String getUsername(Context ctx) {
          return ctx.session().get("userId");
      }
  
      @Override
      public Result onUnauthorized(Context ctx) {
          return redirect(routes.Users.login(ctx.request().uri()));
      }
  
  
  }
  </code></pre>
  
  <p>And when I use the <code>@Security.Authenticated(Secured.class)</code> annotation for controller method it redirects if session with "userId" do not exists.</p>
  
  <p><em>So the question is, how can I fake the session?</em></p>
  
  <p>I tried obviously call <code>Controller.session("usderId", "2");</code></p>
  
  <p>And got an exception:</p>
  
  <pre><code>java.lang.RuntimeException: There is no HTTP Context available from here.
  
      at play.mvc.Http$Context.current(Http.java:30)
      at play.mvc.Controller.session(Controller.java:54)
      at play.mvc.Controller.session(Controller.java:61)
      at controllers.UsersTest.testUnloginedChangePassword(UsersTest.java:35)
  </code></pre>
  
  <p><strong><em>My question is: How to fake the session for controller?</em></strong></p>
  
  <p>And one additional question: how to test routes without using deprecated API, like <code>Result result = routeAndCall(fakeRequest(GET, "/change_password"));</code> ?</p>