Add ScalaTestWithActorTestKitBase trait to handle multiple extends
This commit is contained in:
parent
4b82b53705
commit
3a8d86f8a6
3 changed files with 54 additions and 9 deletions
|
|
@ -38,13 +38,9 @@ import pekko.actor.typed.ActorSystem
|
||||||
* The application.conf of your project is not used in this case.
|
* The application.conf of your project is not used in this case.
|
||||||
* A specific configuration can be passed as constructor parameter.
|
* A specific configuration can be passed as constructor parameter.
|
||||||
*/
|
*/
|
||||||
abstract class ScalaTestWithActorTestKit(testKit: ActorTestKit)
|
abstract class ScalaTestWithActorTestKit(override val testKit: ActorTestKit)
|
||||||
extends ActorTestKitBase(testKit)
|
extends ActorTestKitBase(testKit)
|
||||||
with TestSuite
|
with ScalaTestWithActorTestKitBase {
|
||||||
with Matchers
|
|
||||||
with BeforeAndAfterAll
|
|
||||||
with ScalaFutures
|
|
||||||
with Eventually {
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Config loaded from `application-test.conf` if that exists, otherwise
|
* Config loaded from `application-test.conf` if that exists, otherwise
|
||||||
|
|
@ -74,6 +70,21 @@ abstract class ScalaTestWithActorTestKit(testKit: ActorTestKit)
|
||||||
*/
|
*/
|
||||||
def this(config: Config, settings: TestKitSettings) =
|
def this(config: Config, settings: TestKitSettings) =
|
||||||
this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config, settings))
|
this(ActorTestKit(ActorTestKitBase.testNameFromCallStack(), config, settings))
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A ScalaTest base trait for the [[ActorTestKit]] which [[ScalaTestWithActorTestKit]] extends. If you find yourself in
|
||||||
|
* the situation where you need to extend the same test suite in different ways then you can implement your tests within
|
||||||
|
* a trait that extends [[ScalaTestWithActorTestKitBase]].
|
||||||
|
*/
|
||||||
|
trait ScalaTestWithActorTestKitBase
|
||||||
|
extends TestSuite
|
||||||
|
with Matchers
|
||||||
|
with BeforeAndAfterAll
|
||||||
|
with ScalaFutures
|
||||||
|
with Eventually {
|
||||||
|
|
||||||
|
def testKit: ActorTestKit
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `PatienceConfig` from [[pekko.actor.testkit.typed.TestKitSettings#DefaultTimeout]].
|
* `PatienceConfig` from [[pekko.actor.testkit.typed.TestKitSettings#DefaultTimeout]].
|
||||||
|
|
|
||||||
|
|
@ -15,16 +15,19 @@ package docs.org.apache.pekko.actor.testkit.typed.scaladsl
|
||||||
|
|
||||||
import scala.annotation.nowarn
|
import scala.annotation.nowarn
|
||||||
import docs.org.apache.pekko.actor.testkit.typed.scaladsl.AsyncTestingExampleSpec.Echo
|
import docs.org.apache.pekko.actor.testkit.typed.scaladsl.AsyncTestingExampleSpec.Echo
|
||||||
|
//#extend-multiple-times
|
||||||
//#log-capturing
|
//#log-capturing
|
||||||
import org.apache.pekko
|
import org.apache.pekko
|
||||||
import pekko.actor.testkit.typed.scaladsl.LogCapturing
|
import pekko.actor.testkit.typed.scaladsl.LogCapturing
|
||||||
//#scalatest-integration
|
//#scalatest-integration
|
||||||
import pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit
|
import pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit
|
||||||
import org.scalatest.wordspec.AnyWordSpecLike
|
|
||||||
|
|
||||||
//#scalatest-integration
|
//#scalatest-integration
|
||||||
//#log-capturing
|
//#log-capturing
|
||||||
|
import pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKitBase
|
||||||
|
//#scalatest-integration
|
||||||
|
import org.scalatest.wordspec.AnyWordSpecLike
|
||||||
|
//#scalatest-integration
|
||||||
|
//#extend-multiple-times
|
||||||
|
|
||||||
@nowarn
|
@nowarn
|
||||||
//#scalatest-integration
|
//#scalatest-integration
|
||||||
|
|
@ -54,3 +57,23 @@ class LogCapturingExampleSpec extends ScalaTestWithActorTestKit with AnyWordSpec
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//#log-capturing
|
//#log-capturing
|
||||||
|
|
||||||
|
//#extend-multiple-times
|
||||||
|
|
||||||
|
trait ExtendTestMultipleTimes extends ScalaTestWithActorTestKitBase with AnyWordSpecLike with LogCapturing {
|
||||||
|
"ScalaTestWithActorTestKitBase" must {
|
||||||
|
"behave when extended in different ways" in {
|
||||||
|
val pinger = testKit.spawn(Echo(), "ping")
|
||||||
|
val probe = testKit.createTestProbe[Echo.Pong]()
|
||||||
|
val message = this.getClass.getSimpleName
|
||||||
|
pinger ! Echo.Ping(message, probe.ref)
|
||||||
|
val returnedMessage = probe.expectMessage(Echo.Pong(message))
|
||||||
|
returnedMessage.message.contains("ExtendTestMultipleTimes") shouldBe false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestWithOneImplementation extends ScalaTestWithActorTestKit with ExtendTestMultipleTimes
|
||||||
|
class TestWithAnotherImplementation extends ScalaTestWithActorTestKit with ExtendTestMultipleTimes
|
||||||
|
//#extend-multiple-times
|
||||||
|
|
|
||||||
|
|
@ -137,6 +137,17 @@ Scala
|
||||||
Java
|
Java
|
||||||
: @@snip [AsyncTestingExampleTest.java](/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java) { #junit-integration }
|
: @@snip [AsyncTestingExampleTest.java](/actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java) { #junit-integration }
|
||||||
|
|
||||||
|
As you may have noticed @scaladoc[ScalaTestWithActorTestKit](pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit) is an abstract class
|
||||||
|
which means its problematic if you want treat a given test suite as a value and extend it in multiple ways (i.e. as an example you happen to be using
|
||||||
|
[testcontainers-scala](https://github.com/testcontainers/testcontainers-scala) and hypothetically you want to extend the same test for each different type of database
|
||||||
|
you support).
|
||||||
|
|
||||||
|
If you find yourself in this situation you can instead define your tests within a trait that extends @scaladoc[ScalaTestWithActorTestKitBase](pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKitBase).
|
||||||
|
Since this is a trait you can then have different classes which extend this along with @scaladoc[ScalaTestWithActorTestKit](pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit).
|
||||||
|
|
||||||
|
Scala
|
||||||
|
: @@snip [AsyncTestingExampleSpec.scala](/actor-testkit-typed/src/test/scala/docs/org/apache/pekko/actor/testkit/typed/scaladsl/ScalaTestIntegrationExampleSpec.scala) { #extend-multiple-times }
|
||||||
|
|
||||||
### Configuration
|
### Configuration
|
||||||
|
|
||||||
By default the `ActorTestKit` loads configuration from `application-test.conf` if that exists, otherwise
|
By default the `ActorTestKit` loads configuration from `application-test.conf` if that exists, otherwise
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue