1
votes

I am trying to create a test class to my project that uses Play 2.6 and Scala 2.12. I've imported the scalatest lib:

libraryDependencies += guice
libraryDependencies += evolutions
libraryDependencies += jdbc
libraryDependencies += filters

libraryDependencies += "com.h2database" % "h2" % "1.4.194"
libraryDependencies += "com.typesafe.play" %% "anorm" % "2.5.3"
libraryDependencies += "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.0" % Test
libraryDependencies += "org.scala-lang" % "scala-actors" % "2.10.0-M7" % "test"
libraryDependencies += "org.scalactic" %% "scalactic" % "3.0.4"
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.4" % "test"

libraryDependencies += "com.typesafe.play" %% "play-slick" % "3.0.0"
libraryDependencies += "com.typesafe.play" %% "play-slick-evolutions" % "3.0.0"
libraryDependencies += "org.xerial" % "sqlite-jdbc" % "3.19.3"

And my compiler says that ShouldMatchers does not exist in the scalatest lib.

class RackRepositorySpec extends PlaySpec with GuiceOneAppPerTest with Injecting {

  val database = Databases(
    driver = "org.sqlite.JDBC",
    url = "jdbc:sqlite:development.db",
    name = "default",
    config = Map(
      "username" -> "",
      "password" -> ""
    )
  )
  val guice = new GuiceInjectorBuilder()
    .overrides(bind[Database].toInstance(database))
    .injector()
  val defaultDbProvider = guice.instanceOf[DatabaseConfigProvider]

  def beforeAll() = Evolutions.applyEvolutions(database)
  def afterAll() = {
    // Evolutions.cleanupEvolutions(database)
    database.shutdown()
  }

  Evolution(
    1,
    "create table test (id bigint not null, name varchar(255));",
    "drop table test;"
  )
}

I got the last version of the Scalatest but it seems that this class does not exist anymore. I am following this example: https://dzone.com/articles/getting-started-play-21-scala Does anyone have another example to built a Scala test for Slick in-memory database?

[info] RackRepositorySpec:
[info] models.RackRepositorySpec *** ABORTED ***
[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] 
[info] 1 error
[info]   at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1045)
[info]   at com.google.inject.internal.InjectorImpl.getProvider(InjectorImpl.java:1004)
[info]   at com.google.inject.internal.InjectorImpl.getInstance(InjectorImpl.java:1054)
[info]   at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:409)
[info]   at play.api.inject.guice.GuiceInjector.instanceOf(GuiceInjectorBuilder.scala:404)
[info]   at play.api.inject.ContextClassLoaderInjector.$anonfun$instanceOf$2(Injector.scala:117)
[info]   at play.api.inject.ContextClassLoaderInjector.withContext(Injector.scala:126)
[info]   at play.api.inject.ContextClassLoaderInjector.instanceOf(Injector.scala:117)
[info]   at models.RackRepositorySpec.<init>(RackRepositorySpec.scala:26)
[info]   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

Kind Regards, Felipe

2
Use org.scalatest.Matchers instead.perbellinio

2 Answers

1
votes

ShouldMatchers were deprecated in version 2 and removed in version 3. Use Matchers or MustMatchers instead.

See the release notes.

1
votes

I did use Slick for one of my Scala projects and I had Postgress as my database. For unit testing purposed, I created a test h2 in memory database which is populated during unit test and teared down after the tests are done.

You can have a look at the sample here:

https://github.com/joesan/plant-simulator/blob/master/test/com/inland24/plantsim/services/database/PowerPlantDBServiceSpec.scala

What I also wanted to do is to have some sort of automation where in iI wanted to populate the h2 data files and keep them forever for the next cycles of unit testing.