Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run ScalaTest with Guice DI and Slick?

Tags:

I don't know how to configure GuiceApplicationBuilder in such a way, that I am able to load controllers that require a DatabaseConfigProvider to be injected.

I'd like to specify an alternative postgres database for testing, or an in memory database (if that is possible).

Code

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() )          ...     } } 

Stacktrace

[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]   ... 
like image 739
Taig Avatar asked Jul 31 '15 18:07

Taig


People also ask

What is ScalaTest used for?

Overview. ScalaTest is one of the most popular, complete and easy-to-use testing frameworks in the Scala ecosystem. Where ScalaTest differs from other testing tools is its ability to support a number of different testing styles such as XUnit and BDD out of the box.


1 Answers

You need to add a configuration to your GuiceApplicationBuilder(), then everything should be handled automatically by play framework. Something like this should help:

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() 
like image 111
Mateusz Dymczyk Avatar answered Oct 02 '22 12:10

Mateusz Dymczyk