Search code examples
scalaplayframework-2.2scalatest

When I run tests with scalaTestPlus and Play framework, I get an error "no started application"


I'm trying to make scalaTestPlus work in my play application (I'm using Play! 2.2). It works well until I need a function coming from my application. For instance, if I run this very simple test (by launching in a sbt console "test-only TestName"):

import org.scalatestplus.play._
import org.scalatest._
import Matchers._

class Test extends PlaySpec {
  "This test" must {
    "run this very simple test without problem" in {
      1 mustEqual 1
    }
  }
}

There is no problem, but as soon as I call a function from my app, like in this code:

class Test extends PlaySpec {
  "This test" must {
    "run this very simple test without problem" in {
      models.Genre.genresStringToGenresSet(Option("test")) //Here is the problem 
      1 mustEqual 1
    }
  }
}

I get an error: java.lang.ExceptionInInitializerError: at... Cause: java.lang.RuntimeException: There is no started application (even if my application is running).

I'm probably missing something simple since I'm brand new to ScalaTest, so any help abut what I'm doing wrong would be apreciated ;)


Solution

  • As asked by @akauppi, here is a method that works perfectly for me:

    import org.scalatestplus.play.{OneAppPerSuite, PlaySpec}
    
    class A extends PlaySpec with OneAppPerSuite {
    
      "a" must {
    
        "return true" in {
          //thanks to with OneAppPerSuite, it now works
          models.Genre.genresStringToGenresSet(Option("test"))
          1 mustBe 1
        }
    
        "return false" in {
          1 mustBe 2
        }
      }
    }
    

    And I simply launch the test with sbt ~testOnly a