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

  • SEARCH
  • Home
  • 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 8409631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:57:51+00:00 2026-06-09T23:57:51+00:00

I am trying to learn the unit tests in Play scala, but I am

  • 0

I am trying to learn the unit tests in Play scala, but I am running into some issues. I am trying to run several tests on my models layer like this:

"User Model" should {
    "be created and retrieved by username" in {
        running(FakeApplication()) {
            val newUser = User(username = "weezybizzle",password = "password")
            User.save(newUser)
            User.findOneByUsername("weezybizzle") must beSome
        }
    }
    "another test" in {
        running(FakeApplication()) {
            // more tests involving adding and removing users
        }
    }
}

However when doing things this way, I fail to connect to the database on the second unit test, saying that the connection is closed. I tried to solve this by enclosing all the code in a block that runs on the same fake application, but that didn’t work either.

  running(FakeApplication()) {
    "be created and retrieved by username" in {
        val newUser = User(username = "weezybizzle",password = "password")
        User.save(newUser)
        User.findOneByUsername("weezybizzle") must beSome
    }
    "another test" in {
        // more tests involving adding and removing users
    }
  }
  • 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-06-09T23:57:53+00:00Added an answer on June 9, 2026 at 11:57 pm

    The specs2 tests are performed by default in parallel which may cause problems with accessing databases, especially when you rely on the db contents provided by a previous test. So to force sequential testing you have to tell specs2 to do so:

    class ModelSpec extends Specification with Logging {
      override def is = args(sequential = true) ^ super.is
    ...
    }
    

    For tests done in one FakeApplication you can wrap the whole tests in it:

      running(FakeApp) {
        log.trace("Project tests.")
        val Some(project) = Project.findByName("test1")
    
        "Project" should {
    
          "be retrieved by name" in {
            project must beAnInstanceOf[Project]
            project.description must endWith("project")
          }
    

    The whole sample can be found here. That was my first attempt to deal with problems while testing MongoDB with Play! framework.

    The second approach I borrowed from the salat project, which is by the way a very good source of specs examples dealing with MongoDB (although it is not a Play! framework app). You have to define a trait extending Around and Scope, where you can put anything you need to be initialized in an application instance:

    import org.specs2.mutable._
    import org.specs2.execute.StandardResults
    
    import play.api.mvc._
    import play.api.mvc.Results
    import play.api.test._
    import play.api.test.Helpers._
    
    trait FakeApp extends Around with org.specs2.specification.Scope {
    
      val appCfg = Map(
        "first.config.key" -> "a_value",
        "second.config.key" -> "another value"
      )
    
      object FakeApp extends FakeApplication(
          additionalPlugins = Seq("com.github.rajish.deadrope.DeadropePlugin"),
          additionalConfiguration = appCfg
        ) {
        // override val routes = Some(Routes)
      }
    
      def around[T <% org.specs2.execute.Result](test: => T) = running(FakeApp) {
        Logger.debug("Running test ==================================")
        test  // run tests inside a fake application
      }
    }
    

    Edit 2013-06-30:

    In the current version of specs2 the around signature should be:

    def around[T : AsResult](test: => T): Result
    

    End of edit

    Then a test can be written like that:

    class SomeSpec extends Specification { sequential // according to @Eric comment
    
      "A test group" should {
        "pass some tests" in new FakeApp {
          1 must_== 1
        }
    
        "and these sub-tests too" in {
          "first subtest" in new FakeApp {
             success
          }
          "second subtest" in new FakeApp {
             failure
          }
        }
      }
    }
    

    A full sample of such suite can be found here.

    On a final note: It’s also good to clean up the test database before starting a suite:

      step {
        MongoConnection().dropDatabase("test_db")
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to learn unit testing. I am trying to unit test some
I'm currently trying to learn proper unit-test. So now I'm trying to write unit-tests
I am trying to learn unit tests. Following is my first ever written unit
I was trying to learn unit test with PHP. I know it's a bit
I am trying to learn about unit testing in Zend Framework. I have installed
Trying to learn some R after doing mostly Haskell for rather a long time
I'm trying to learn Spring Batch , but the startup guide is very confusing.
I'm trying to learn dependency injection and have come across a problem, when unit
I am trying to learn how to use Moq to do unit testing with
I m trying to learn scala these days. I get confused with _ operator.

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.