Add ScalaTestWithActorTestKitBase trait to handle multiple extends

This commit is contained in:
Matthew de Detrich 2023-04-12 12:54:17 +02:00 committed by Matthew de Detrich
parent 4b82b53705
commit 3a8d86f8a6
3 changed files with 54 additions and 9 deletions

View file

@ -38,13 +38,9 @@ import pekko.actor.typed.ActorSystem
* 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)
abstract class ScalaTestWithActorTestKit(override val testKit: ActorTestKit)
extends ActorTestKitBase(testKit)
with TestSuite
with Matchers
with BeforeAndAfterAll
with ScalaFutures
with Eventually {
with ScalaTestWithActorTestKitBase {
/**
* 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) =
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]].

View file

@ -15,16 +15,19 @@ package docs.org.apache.pekko.actor.testkit.typed.scaladsl
import scala.annotation.nowarn
import docs.org.apache.pekko.actor.testkit.typed.scaladsl.AsyncTestingExampleSpec.Echo
//#extend-multiple-times
//#log-capturing
import org.apache.pekko
import pekko.actor.testkit.typed.scaladsl.LogCapturing
//#scalatest-integration
import pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKit
import org.scalatest.wordspec.AnyWordSpecLike
//#scalatest-integration
//#log-capturing
import pekko.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKitBase
//#scalatest-integration
import org.scalatest.wordspec.AnyWordSpecLike
//#scalatest-integration
//#extend-multiple-times
@nowarn
//#scalatest-integration
@ -54,3 +57,23 @@ class LogCapturingExampleSpec extends ScalaTestWithActorTestKit with AnyWordSpec
}
}
//#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