如何为每个测试方案加载配置?

时间:2019-06-10 09:21:22

标签: scala akka

我正在使用Akka,想测试我的演员。该测试如下所示:

import akka.testkit._
import com.sweetsoft._
import org.scalatest.Assertion
import com.typesafe.config.{Config, ConfigFactory}
import org.testcontainers.containers.KafkaContainer

import concurrent.duration._

final class DetectorSpec extends BddSpec {


  private val sapMock = new SapMock()
    .withExposedPorts(8080)

  private val kafkaMock = new KafkaContainer()

  private val config: String => String => Config = kafka => sap =>
    ConfigFactory.parseString(
      s"""
         |kafka {
         |  servers = "$kafka"
         |}
         |
      |sap {
         |  server = "ws://${sap}"
         |}
         |
  """)


  private val listener1 = TestProbe()
  private val listener2 = TestProbe()

  private val detector = system.actorOf(DetectorSupervisor.props)

  after {
  }

  override def beforeAll(): Unit = {

  }


  override def afterAll(): Unit = {
    TestKit.shutdownActorSystem(system)
  }


  feature("Detect Kafka and SAP availability") {
    info("As a technical user, I want to be notified in real time, if Kafka and SAP is up and running or not.")
    scenario("SAP and Kafka are offline") {


      Given("I am registering two listeners")
      detector ! AddNewListener(listener1.ref)
      detector ! AddNewListener(listener2.ref)
      When("I am receive the state message")
      val res1 = listener1.expectMsgPF[Assertion](2.second) _
      val res2 = listener2.expectMsgPF[Assertion](2.second) _
      Then("it should contain `Kafka and SAP are offline`")
      res1 {
        case status: ServerStatus =>
          status.health should be(ServersOffline)
      }
      res2 {
        case status: ServerStatus =>
          status.health should be(ServersOffline)
      }
    }

    scenario("SAP is online and Kafka is offline") {
      sapMock.start()
      Given("I am waiting for the current state message")
      detector ! AddNewListener(listener1.ref)
      When("I am receive the state message")
      val res1 = listener1.expectMsgPF[Assertion](2.second) _
      Then("it should contain `Kafka is offline`")
      res1 {
        case status: ServerStatus =>
          sapMock.stop()
          status.health should be(ServersOffline)
      }
    }

    scenario("SAP is offline and Kafka is online") {
      Given("I am waiting for the current state message")
      When("I am receive the state message")
      Then("it should contain `SAP is offline`")
      cancel()
    }

    scenario("SAP and Kafka are available") {
      Given("I am waiting for the current state message")
      When("I am receive the state message")
      Then("it should contain `SAP and Kafka are online`")
      cancel()
    }
  }
}

如您所见,我正在使用testcontainer构建测试环境。 我只想在特定情况下启动容器,并且在该情况下,我想注入configuration

例如,场景scenario("SAP and Kafka are offline")scenario("SAP is online and Kafka is offline")具有不同的配置。

问题是,如何在不同的情况下加载不同的配置?

在akka网站上,它显示了如何按以下方式加载配置:

import akka.actor.ActorSystem
import com.typesafe.config.ConfigFactory
val customConf = ConfigFactory.parseString("""
  akka.actor.deployment {
    /my-service {
      router = round-robin-pool
      nr-of-instances = 3
    }
  }
  """)
// ConfigFactory.load sandwiches customConfig between default reference
// config and default overrides, and then resolves it.
val system = ActorSystem("MySystem", ConfigFactory.load(customConf))

为什么必须以这种方式执行此操作,因为只有在启动容器时容器端口才可用,并且我不想在每种情况下都启动容器。

BddSpec类:

abstract class BddSpec extends TestKit(ActorSystem("PluggerSystem"))
  with AsyncFeatureSpecLike
  with Matchers
  with GivenWhenThen
  with BeforeAndAfter
  with BeforeAndAfterAll

0 个答案:

没有答案