Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:50:13+00:00 2026-05-31T23:50:13+00:00

I’m looking for a way to preserve the session when using Play 2.0’s fakeRequest

  • 0

I’m looking for a way to preserve the session when using Play 2.0’s fakeRequest in my Java tests, but my attempts fail while invoking methods in the Scala-based JARs.

Based on a pull request mentioned in the Scala question Add values to Session during testing (FakeRequest, FakeApplication), I figured the following might work in Java:

public Session getSession(Result result) {
  play.api.mvc.Cookies scalaCookies =
      play.api.test.Helpers.cookies(result.getWrappedResult());
  play.api.mvc.Cookie scalaSessionCookie =
      scalaCookies.get(play.api.mvc.Session.COOKIE_NAME()).get();
  scala.Option<play.api.mvc.Cookie> optionalCookie =
      scala.Option.apply(scalaSessionCookie);

  // Compiles fine, but fails with NoSuchMethodError:
  play.api.mvc.Session scalaSession =
      play.api.mvc.Session.decodeFromCookie(optionalCookie);

  return new play.mvc.Http.Session(Scala.asJava(scalaSession.data()));
}

This compiles just fine, but while running the tests it gets me:

java.lang.NoSuchMethodError: 
  play.api.mvc.Session.decodeFromCookie(Lscala/Option;)Lplay/api/mvc/Session;

Being a total Scala newby, I really have no idea if I’m even close. The Scala Session does expose (trait) that method through CookieBaker, I think.

Note that I am not necessarily looking for a way to get the above code running; the above is really just the first (possible) step to get the session. Next I’d probably try to use something like play.api.mvc.Session.encodeAsCookie(session) to pass it to the subsequent requests. Like for the ZenTasks demo:

@Test
public void testLoginAndMore() {
  Helpers.running(Helpers.fakeApplication(Helpers.inMemoryDatabase()), 
  new Runnable() {
    public void run() {
      Map<String, String> data = new HashMap<String, String>();
      data.put("email", "guillaume@sample.com");
      data.put("password", "secret");

      Result result = 
        callAction(controllers.routes.ref.Application.authenticate(),
          fakeRequest().withFormUrlEncodedBody(data));
      assertThat(status(result)).isEqualTo(Status.SEE_OTHER);
      assertThat(redirectLocation(result)).isEqualTo("/");

      // All fine; we're logged in. Now somehow preserve the cookie. This
      // does NOT do the trick:
      Session session = getSession(result);
      // ...subsequent callAction(..)s, somehow passing the session cookie 
    }
  });
}

For 1.x, Playframework Secure module: how do you “log in” to test a secured controller in a FunctionalTest? helps, but things seem to have changed in 2.0, and I never used 1.x.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:50:14+00:00Added an answer on May 31, 2026 at 11:50 pm

    Not much magic needed after all. The following simply preserves the HTTP header that sets the cookies, and passes that in the next request:

    Map<String, String> data = new HashMap<String, String>();
    data.put("email", "guillaume@sample.com");
    data.put("password", "secret");
    
    Result result = callAction(controllers.routes.ref.Application.authenticate(),
      fakeRequest().withFormUrlEncodedBody(data));
    
    assertThat(status(result)).isEqualTo(Status.SEE_OTHER);
    assertThat(redirectLocation(result)).isEqualTo("/");
    // All fine; we're logged in. Preserve the cookies:
    String cookies = header(HeaderNames.SET_COOKIE, result);
    
    // Fetch next page, passing the cookies
    result = routeAndCall(fakeRequest(GET, redirectLocation(result))
      .withHeader(HeaderNames.COOKIE, cookies));
    
    assertThat(status(result)).isEqualTo(Status.OK);
    assertThat(contentAsString(result).contains("Guillaume Bort"));
    

    (See the first version of this very answer for some information about getting only the PLAY_SESSION cookie, and parsing that. That’s hardly needed though.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.