我不知道如何以这种方式配置GuiceApplicationBuilder
,我可以加载需要注入DatabaseConfigProvider
的控制器。
我想指定一个替代的 postgres 数据库进行测试,或者在内存数据库中指定(如果可能的话)。
代码
class User
extends MySpecs
with OneAppPerTest
{
override def newAppForTest( testData: TestData ) = new GuiceApplicationBuilder()
// Somehow bind a database here, I guess?
.build()
"A test" should "test" in
{
val result = Application.instanceCache[api.controller.User]
.apply( app )
.list()( FakeRequest() )
...
}
}
堆栈跟踪
[info] - should return an entity *** FAILED ***
[info] com.google.inject.ConfigurationException: Guice configuration errors:
[info]
[info] 1) No implementation for play.api.db.slick.DatabaseConfigProvider was bound.
[info] while locating play.api.db.slick.DatabaseConfigProvider
[info] for parameter 1 at api.controller.User.<init>(User.scala:22)
[info] while locating api.controller.User
[info]
[info] 1 error
[info] at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1042)
[info] at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1001)
[info] at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1051)
[info] at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:321)
[info] at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:316)
[info] at play.api.Application$$anonfun$instanceCache$1.apply(Application.scala:234)
[info] at play.api.Application$$anonfun$instanceCache$1.apply(Application.scala:234)
[info] at play.utils.InlineCache.fresh(InlineCache.scala:69)
[info] at play.utils.InlineCache.apply(InlineCache.scala:55)
[info] ...
答案 0 :(得分:7)
您需要向GuiceApplicationBuilder()
添加配置,然后应该通过play框架自动处理所有内容。这样的事情应该会有所帮助:
val app = new GuiceApplicationBuilder()
.configure(
Configuration.from(
Map(
"slick.dbs.YOURDBNAME.driver" -> "slick.driver.H2Driver$",
"slick.dbs.YOURDBNAME.db.driver" -> "org.h2.Driver",
"slick.dbs.YOURDBNAME.db.url" -> "jdbc:h2:mem:",
"slick.dbs.default.driver" -> "slick.driver.MySQLDriver$",
"slick.dbs.default.db.driver" -> "com.mysql.jdbc.Driver"
)
)
)
.in(Mode.Test)
.build()
答案 1 :(得分:2)
这种方法有一些设置,但最终结果是公平的。首先,通过扩展它来实现您自己的GuiceApplicationLoader
。查看我的answer how to implement it。你自己的应用程序加载器您可以为每个Prod/Dev/Test
模式指定不同的配置/模块 - 以及不同的数据源。您的主application.conf
无法配置数据源。相反,您可以将其移动到环境特定的配置,无论如何,这些配置将通过应用程序加载器与主配置合并。您的dev.conf
看起来如下:
slick.dbs {
default {
driver = "slick.driver.PostgresDriver$",
db {
driver = "org.postgresql.Driver",
url = "jdbc:postgresql://localhost:5432/dev",
user = "postgres"
password = "postgres"
}
}
}
现在的诀窍是使用相同的数据源名称,在本例中为default
,用于所有其他配置(数据库URL,驱动程序,凭据等将不同)。通过这样的设置,您的evolutions
将应用于您的测试和开发数据库。您的test.conf
可能如下所示:
slick.dbs {
default {
// in memory configuration
}
}
在您的测试中,将WithApplicationLoader
与您的自定义应用程序加载器一起使用,即可。
@RunWith(classOf[JUnitRunner])
class ApplicationSpec extends Specification {
"Application" should {
"return text/html ok for home" in new WithApplicationLoader(new CustomApplicationLoader) {
val home = route(FakeRequest(routes.ApplicationController.home())).get
status(home) must equalTo(OK)
contentType(home) must beSome.which(_ == "text/html")
}
}
}
在测试中,您可以访问app: Application
值:
val service = app.injector.instanceOf(classOf[IService])