Getting session value from ScalatraTest-ScalaTest

I am currently writing several Scalatra tests using the ScalaTest framework and the ScalatraSuite class.

  test("when i try to go to the base url it shold redirect me "){
    get("/") {
      status should be(302) 


The next step requires me to check for the existence of some session values, but it's not clear how to do this? Can anyone advise? I'm creating a SessionAccess trait, which for test purposes, overriding with a simple dash the stores session in the available HashMap, but I'm sure there is an easier way?


source to share

1 answer

I've looked at the ScalatraSuite code and it looks like there is no way to get the session object itself. However, you can make multiple calls within a session to test the expected behavior.

If you had the following calls:

post("/start") {
  session("foo") = params("foo")
  // ...

get("/do_something") {


you can check it like this:

test("Whatever inside of a session") {
  session {
    post("/start", "foo" -> "bar") {
      // assert...
    get("/do_something") {
      body should equal ("bar")


Hope it helps.



All Articles