/
ScalaTestWithActorTestKit.scala
74 lines (65 loc) · 2.78 KB
/
ScalaTestWithActorTestKit.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
* Copyright (C) 2018-2019 Lightbend Inc. <https://www.lightbend.com>
*/
package akka.actor.testkit.typed.scaladsl
import akka.actor.testkit.typed.TestKitSettings
import com.typesafe.config.Config
import com.typesafe.config.ConfigFactory
import org.scalatest._
import org.scalatest.concurrent.Eventually
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.time.Span
/**
* A ScalaTest base class for the [[ActorTestKit]], making it possible to have ScalaTest manage the lifecycle of the testkit.
* The testkit will be automatically shut down when the test completes or fails using ScalaTest's BeforeAndAfterAll trait. If
* a spec overrides afterAll it must call super.afterAll.
*
* Note that ScalaTest is not provided as a transitive dependency of the testkit module but must be added explicitly
* to your project to use this.
*
* By default config is loaded from `application-test.conf` if that exists, otherwise
* using default configuration from the reference.conf resources that ship with the Akka libraries.
* The application.conf of your project is not used in this case.
* A specific configuration can be passed as constructor parameter.
*/
abstract class ScalaTestWithActorTestKit(testKit: ActorTestKit)
extends ActorTestKitBase(testKit)
with TestSuite
with Matchers
with BeforeAndAfterAll
with ScalaFutures
with Eventually {
/**
* Config loaded from `application-test.conf` if that exists, otherwise
* using default configuration from the reference.conf resources that ship with the Akka libraries.
* The application.conf of your project is not used in this case.
*/
def this() = this(ActorTestKit(ActorTestKitBase.testNameFromCallStack()))
/**
* Use a custom config for the actor system.
*/
def this(config: String) =
this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), ConfigFactory.parseString(config)))
/**
* Use a custom config for the actor system.
*/
def this(config: Config) = this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config))
/**
* Use a custom config for the actor system, and a custom [[akka.actor.testkit.typed.TestKitSettings]].
*/
def this(config: Config, settings: TestKitSettings) =
this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config, settings))
/**
* `PatienceConfig` from [[akka.actor.testkit.typed.TestKitSettings#DefaultTimeout]]
*/
implicit val patience: PatienceConfig =
PatienceConfig(testKit.testKitSettings.DefaultTimeout.duration, Span(100, org.scalatest.time.Millis))
/**
* Shuts down the ActorTestKit. If override be sure to call super.afterAll
* or shut down the testkit explicitly with `testKit.shutdownTestKit()`.
*/
override protected def afterAll(): Unit = {
super.afterAll()
testKit.shutdownTestKit()
}
}