Skip to content

Instantly share code, notes, and snippets.

@vito-c
Last active September 4, 2017 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vito-c/336c468b73f6974a4b2a649899b8b145 to your computer and use it in GitHub Desktop.
Save vito-c/336c468b73f6974a4b2a649899b8b145 to your computer and use it in GitHub Desktop.
Dao Test
package com.blah.database.dao
import java.lang.Thread
import play.api.Application
import org.scalatest.{FunSuite, Matchers, BeforeAndAfter}
import play.api.{Application, ApplicationLoader, Environment, Mode}
import com.dimafeng.testcontainers.{ForAllTestContainer, GenericContainer}
import org.testcontainers.containers.wait.Wait
import org.scalatestplus.play.FakeApplicationFactory
import play.api.db.{Database, DBComponents, HikariCPComponents}
import play.api.db.evolutions.EvolutionsComponents
import play.api._
import play.api.routing.Router
import router.{Routes => PlayRoutes}
import com.softwaremill.macwire._
import scala.concurrent.ExecutionContext
class TestThingDaoSpec
extends FunSuite
with ForAllTestContainer
with FakeTestApplication
with Matchers {
override implicit def executionContext: ExecutionContext = executionContext
override protected lazy val prefix: String = "/"
override lazy val router: Router = wire[PlayRoutes]
/**
* Currently docker-java doesn't support the latest version of docker authentication
* as a result we have to resetup the entire image here.
* @see https://github.com/docker-java/docker-java/pull/727
*/
override val container = GenericContainer(
"postgres:9",
env = Map(
"POSTGRES_USER" -> "test",
"POSTGRES_PASSWORD" -> "supersecret",
"POSTGRES_DB" -> "test"),
exposedPorts = Seq(5432))
test("bologna") {
assert(false == true)
}
}
/**
* @see https://github.com/playframework/play-scala-compile-di-example
* Setup a fake application to use with testing.
*/
trait FakeTestApplication extends FakeApplicationFactory {
override def context: play.api.ApplicationLoader.Context = {
val env = Environment.simple()
ApplicationLoader.createContext(env, initialSettings = testSettings)
}
def testSettings = {
Map(
"db.test.url" -> "jdbc:postgresql://5432:5432/test",
"db.test.jdbcUrl" -> "jdbc:postgresql://5432:5432/test",
"db.test.databaseName" -> "test",
"db.test.driver" -> "org.postgresql.Driver",
"db.test.threadPoolSize" -> "10",
"db.test.username" -> "test",
"db.test.password" -> "supersecure")
}
override def fakeApplication: Application = {
val appLoader = ApplicationLoader.apply(context)
appLoader.load(context)
}
}
class TestApplicationLoader extends ApplicationLoader {
private var components: TestComponents = _
def load(context: ApplicationLoader.Context): Application = {
components = new TestComponents(context)
components.application
}
}
class TestComponents(context: ApplicationLoader.Context)
extends BuiltInComponentsFromContext(context)
with DBComponents
with EvolutionsComponents
with HikariCPComponents {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment