在Play 2.2, Spec2测试中,我得到了配置错误[无法连接到数据库[默认]]

时间:2023-01-19 23:13:23

I am using Scala Spec2 in Play Framework version 2.2 application. When I run the tests, I get following errors:

我在Play Framework 2.2应用程序中使用Scala Spec2。当我运行测试时,我得到以下错误:

$ test-only ApplicationSpec
Mode is Test, Loading: application.test.conf
Create schema
Populate the application schema...
Stopping the application...
Stopping the application...
Stopping the application...
[info] ApplicationSpec
[info] Application should
[info] + send 404 on a bad request
[info] ! render the login page
[error]     anon$1: Configuration error[Cannot connect to database [default]] (Configuration.scala:92)
[error] play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:92)
[error] play.api.Configuration.reportError(Configuration.scala:570)
[error] play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:252)
[error] play.api.db.BoneCPPlugin$$anonfun$onStart$1.apply(DB.scala:243)
[error] play.api.db.BoneCPPlugin.onStart(DB.scala:243)
[error] play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88)
[error] play.api.Play$$anonfun$start$1$$anonfun$apply$mcV$sp$1.apply(Play.scala:88)
[error] play.api.Play$$anonfun$start$1.apply$mcV$sp(Play.scala:88)
[error] play.api.Play$$anonfun$start$1.apply(Play.scala:88)
[error] play.api.Play$$anonfun$start$1.apply(Play.scala:88)
[error] play.utils.Threads$.withContextClassLoader(Threads.scala:18)
[error] play.api.Play$.start(Play.scala:87)
[error] play.api.test.PlayRunners$class.running(Helpers.scala:44)
[error] play.api.test.Helpers$.running(Helpers.scala:364)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$12.apply(ApplicationSpec.scala:25)
[error] ApplicationSpec$$anonfun$1$$anonfun$apply$12.apply(ApplicationSpec.scala:25)

MockGlobal.scala contains following code:

MockGlobal。scala包含以下代码:

object MockGlobal extends GlobalSettings {

  override def onStart(app: Application) {
    super.onStart(app)
    // check if the database is already setup
    implicit val a = app

    if (isDatabaseEmpty(app)) {
      println("Create schema")
      FixtureUtils.populateSchema
      println("Pouplate fixtures ")
      FixtureUtils.GoldData.populateFixtures
    }
  }

  def isDatabaseEmpty(app: Application) = {

    implicit val a = app

    DB.withSession { implicit session =>
      val lst = MTable.getTables("users").list()
      lst.isEmpty
    }
  }

  override def onLoadConfig(config: Configuration,
    path: File, classloader: ClassLoader,
    mode: Mode.Mode): Configuration = {

    val configfile = s"application.${mode.toString.toLowerCase}.conf"
    println(s"Mode is ${mode.toString}, Loading: ${configfile}")
    val modeSpecificConfig = config ++ Configuration(
      ConfigFactory.load(configfile))
    super.onLoadConfig(modeSpecificConfig, path, classloader, mode)
  }

  override def onStop(app: Application) {
    println("Stopping the application...")
  }
}

TestApplication.scala contains following code:

TestApplication。scala包含以下代码:

object TestApplication {
  val log = play.Logger.of("application")
  lazy val app = FakeApplication(withGlobal = Some(MockGlobal))
}

ApplicationSpec.scala contains following code:

ApplicationSpec。scala包含以下代码:

import org.specs2.mutable._
import org.specs2.runner._
import org.junit.runner._
import play.api.test._
import play.api.test.Helpers._
import models.User
import models.Users
import fixtures.TestApplication
import fixtures.FixtureUtils

/**
 * Add your spec here.
 * You can mock out a whole application including requests, plugins etc.
 * For more information, consult the wiki.
 */
@RunWith(classOf[JUnitRunner])
class ApplicationSpec extends Specification {

  "Application" should {

    "send 404 on a bad request" in new WithApplication(TestApplication.app) {
      route(FakeRequest(GET, "/boum")) must beNone
    }

    "render the login page" in running(TestApplication.app) {
      val home = route(FakeRequest(GET, "/")).get
      // must redirect to login page
      status(home) must equalTo(SEE_OTHER)
      val x = redirectLocation(home).map { location =>
        location must equalTo("/login")
        val loginpage = route(FakeRequest(GET, "/login")) map { result =>
          status(result) must equalTo(OK)
          contentType(result) must beSome.which(_ == "text/html")
          contentAsString(result) must contain("Apps: Login")
        }
      }

      x must beSome
    }
  }
}

I have seen similar error reported here:

我在这里看到了类似的错误报告:

I tried adding following line to build.sbt to not fork for each test. This didn't help either:

我试着添加以下的代码来构建。sbt不要每次考试都用叉子。这并没有起到任何帮助:

Keys.fork in (Test) := false

Note two things:

注意两件事:

  • Actual error: Configuration error[Cannot connect to database [default]] (Configuration.scala:92)
  • 实际错误:配置错误[无法连接到数据库[默认]]](Configuration.scala:92)
  • This appears three times Stopping the application...
  • 这将出现三次停止应用程序……

What can I do to solve this issue ?

我能做些什么来解决这个问题?

1 个解决方案

#1


4  

I was trying to avoid creating a new application each time a test is run, so I used:

我试图避免在每次测试运行时创建一个新的应用程序,所以我使用:

lazy val app = FakeApplication(withGlobal = Some(MockGlobal))

However this is the wrong approach. For a forked test, once it has finished, the application is stopped, thereby making it unavailable for other tests. This is the reason I was getting database connection errors in all but the first test that executed.

然而,这是错误的做法。对于分叉测试,一旦完成,应用程序就被停止,从而使它不能用于其他测试。这就是为什么我在除第一个测试之外的所有数据库连接错误中。

I changed the spec test definition

我更改了spec测试定义

  • from this: in new WithApplication(TestApplication.app)
  • 从这里:在new WithApplication(TestApplication.app)中
  • to this: in running(FakeApplication(withGlobal = Some(MockGlobal)))
  • 为此:在运行中(FakeApplication(withGlobal = Some(MockGlobal)))

Here is how it looks now:

现在的情况是:

"render the login page" in running(FakeApplication(withGlobal = Some(MockGlobal))) {
  val home = route(FakeRequest(GET, "/")).get
  // must redirect to login page
  status(home) must equalTo(SEE_OTHER)
  val x = redirectLocation(home).map { location =>
    location must equalTo("/login")
    val loginpage = route(FakeRequest(GET, "/login")) map { result =>
      status(result) must equalTo(OK)
      contentType(result) must beSome.which(_ == "text/html")
      contentAsString(result) must contain("Apps: Login")
    }
  }

  x must beSome
}

Now all the testcases are working fine. I am not seeing any error Configuration error[Cannot connect to database [default]].

现在所有的测试用例都运行良好。我没有看到任何错误配置错误[无法连接到数据库[默认]]]。

#1


4  

I was trying to avoid creating a new application each time a test is run, so I used:

我试图避免在每次测试运行时创建一个新的应用程序,所以我使用:

lazy val app = FakeApplication(withGlobal = Some(MockGlobal))

However this is the wrong approach. For a forked test, once it has finished, the application is stopped, thereby making it unavailable for other tests. This is the reason I was getting database connection errors in all but the first test that executed.

然而,这是错误的做法。对于分叉测试,一旦完成,应用程序就被停止,从而使它不能用于其他测试。这就是为什么我在除第一个测试之外的所有数据库连接错误中。

I changed the spec test definition

我更改了spec测试定义

  • from this: in new WithApplication(TestApplication.app)
  • 从这里:在new WithApplication(TestApplication.app)中
  • to this: in running(FakeApplication(withGlobal = Some(MockGlobal)))
  • 为此:在运行中(FakeApplication(withGlobal = Some(MockGlobal)))

Here is how it looks now:

现在的情况是:

"render the login page" in running(FakeApplication(withGlobal = Some(MockGlobal))) {
  val home = route(FakeRequest(GET, "/")).get
  // must redirect to login page
  status(home) must equalTo(SEE_OTHER)
  val x = redirectLocation(home).map { location =>
    location must equalTo("/login")
    val loginpage = route(FakeRequest(GET, "/login")) map { result =>
      status(result) must equalTo(OK)
      contentType(result) must beSome.which(_ == "text/html")
      contentAsString(result) must contain("Apps: Login")
    }
  }

  x must beSome
}

Now all the testcases are working fine. I am not seeing any error Configuration error[Cannot connect to database [default]].

现在所有的测试用例都运行良好。我没有看到任何错误配置错误[无法连接到数据库[默认]]]。