我希望通过旋转内存数据库来集成测试应用程序。我曾经认为这可能不会太依赖于依赖注入:
class TreatySpec extends PlaySpec with OneAppPerSuite with Results {
val testConf = Configuration(ConfigFactory.load("test.conf"))
override lazy val app = new GuiceApplicationBuilder().in(Mode.Test).loadConfig(testConf).bindings(new TestModule()).build()
"Routes" should {
"404 on a bad request" in {
route(app, FakeRequest(GET, "/boum")).map(status(_)) mustBe Some(NOT_FOUND)
}
}
}
执行时,我收到以下错误:
[info] TreatySpec:
[info] Exception encountered when attempting to run a suite with class name: TreatySpec *** ABORTED ***
[info] com.typesafe.config.ConfigException$Missing: No configuration setting found for key 'slick.dbs.h2'
这是我的test.conf
:
slick.dbs.h2.driver="slick.driver.H2Driver$"
slick.dbs.h2.db.driver="org.h2.Driver"
slick.dbs.h2.db.url="jdbc:h2:mem:test"
slick.dbs.h2.db.username=""
slick.dbs.h2.db.password=""
slick.dbs.h2 {
driver="slick.driver.H2Driver$"
db.driver="org.h2.Driver"
db.url="jdbc:h2:mem:spotRepo"
db.username=""
db.password=""
}
slick.dbs{
h2.driver="slick.driver.H2Driver$"
h2.driver="org.h2.Driver"
h2.url="jdbc:h2:mem:spotRepo"
h2.username=""
h2.password=""
}
我认为Guice模块应该加载配置?无论我做什么,它都不会成为配置中的相关密钥。
class TestModule extends AbstractModule {
override def configure() = {
// Use the system clock as the default implementation of Clock
bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
}
val dbConfigString = "slick.dbs.h2"
@Provides
val dbConfig : DatabaseConfig[JdbcProfile] = DatabaseConfig.forConfig[JdbcProfile](dbConfigString)
}
有人能指出我在正确的方向吗?我做错了什么,以至于播放拒绝加载配置?