sql >> Database >  >> NoSQL >> MongoDB

embedmongo met reactiefmongo-proces wordt niet afgesloten

In play2.4 probeerde ik het zoals hieronder en het werkte.

afhankelijkheden:

    "org.reactivemongo" %% "play2-reactivemongo" % "0.11.7.play24",
    "org.reactivemongo" %% "reactivemongo-extensions-json" % "0.11.7.play24",
    // test
    "org.scalatest" %% "scalatest" % "2.2.4" % Test,
    "de.flapdoodle.embed" % "de.flapdoodle.embed.mongo" % "1.50.0" % Test,
    "org.mockito" % "mockito-core" % "1.10.19" % Test

TestMongoSetup:

import de.flapdoodle.embed.mongo.config.{Net, MongodConfigBuilder}
import de.flapdoodle.embed.mongo.distribution.Version
import de.flapdoodle.embed.mongo.{MongodStarter, MongodProcess, MongodExecutable}
import de.flapdoodle.embed.process.runtime.Network
import org.mockito.Mockito._
import play.modules.reactivemongo.ReactiveMongoApi
import reactivemongo.api.MongoConnection.ParsedURI
import reactivemongo.api.{MongoConnectionOptions, MongoDriver, MongoConnection}
import scala.concurrent.ExecutionContext


trait TestMongoSetup  {
  private var port : Int = _
  private var mongodExe: MongodExecutable = _
  private var mongodProcess: MongodProcess = _
  def start(intiAtPort: Int): Unit = {
    port=intiAtPort
    mongodExe = MongodStarter.getDefaultInstance.prepare(
      new MongodConfigBuilder()
        .version(Version.Main.V3_0)
        .net(new Net(port, Network.localhostIsIPv6()))
        .build()
    )
    mongodProcess = mongodExe.start()
  }
  def stop(): Unit = {
    mongodProcess.stop()
    mongodExe.stop()
  }
  def createConnection(): MongoConnection = {
    val driver = new MongoDriver
    driver.connection(ParsedURI(
      hosts = List(("localhost", port)),
      options = MongoConnectionOptions(),
      ignoredOptions = List.empty[String],
      db = None,
      authenticate = None
    ))
  }
  def createMockedReactiveMongoApi(dbName: String)(implicit ctx: ExecutionContext): ReactiveMongoApi = {
    val connection = createConnection()
    val db = connection(dbName)
    val api = mock(classOf[ReactiveMongoApi])
    doReturn(db).when(api).db
    doReturn(connection).when(api).connection
    api
  }
}

TestClass:

import db.TestMongoSetup
import models.dao.UserDAO
import org.scalatest._
import play.modules.reactivemongo.ReactiveMongoApi
import scala.concurrent.Await
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.Duration

class UserServiceTest extends FlatSpec with ShouldMatchers with GivenWhenThen with BeforeAndAfterAll with TestMongoSetup {  private var mockedAPI: ReactiveMongoApi = _
  var dao: UserDAO = _
  val port : Int = 12345
  override def beforeAll(): Unit = {
    start(port)
    mockedAPI = createMockedReactiveMongoApi("testDB")
    dao = new UserDAO(mockedAPI)
  }
  override def afterAll(): Unit = {
    mockedAPI.connection.actorSystem.shutdown()
    mockedAPI.connection.actorSystem.awaitTermination()
    stop()
  }
  "Check" should "check User object into DB" in {
    Given("a user info")
    val email = "[email protected]"
    val pwd= "abcddd"
    When("it fetch from DB")
    val fromDBUser = Await.result(dao.fetch(email,pwd), Duration.Inf)
    Then("it should be fetched")
    fromDBUser.get.email  should equal(email)
  }
}


  1. Geografisch verspreide MongoDB-clusters op AWS in de EU-regio

  2. Hoe ansible mongodb_user,mongodb_replicaset modules werken?

  3. Hoe kan ik een lege array in mongodb opslaan met js

  4. MongoDB-paginering op arrays - Controleer of $ slice het begin van de array heeft bereikt