Akka Projections provides a TestKit to ease testing. There are two supported styles of test: running with an assert function and driving it with an Akka Streams TestKit TestSink probe.
Dependencies
The Akka dependencies are available from Akka’s library repository. To access them there, you need to configure the URL for this repository.
When testing with an assert function the Projection is started and stopped by the TestKit. While the projection is running, the assert function will be called until it completes without errors (no exceptions or assertion errors are thrown).
In the example below the Projection will update a CartView. The test will run until it observes that the CartView for id abc-def is available in the repository.
sourceimport akka.actor.testkit.typed.scaladsl.ScalaTestWithActorTestKitimport akka.projection.testkit.scaladsl.ProjectionTestKit
projectionTestKit.run(projection){// confirm that cart checkout was inserted in db
cartViewRepository.findById("abc-def").futureValue
}
By default, the test will run for 3 seconds. The assert function will be called every 100 milliseconds. Those values can be modified programatically.
Note: when testing a Projection with this method, the Restart Backoff is disabled. Any backoff configuration settings from .conf file or programmatically added will be overwritten.
sourceimport scala.concurrent.duration._
projectionTestKit.run(projection, max =5.seconds, interval =300.millis){// confirm that cart checkout was inserted in db
cartViewRepository.findById("abc-def").futureValue
}
The Akka Stream TestKit can be used to drive the pace of envelopes flowing through the Projection.
The Projection starts as soon as the first element is requested by the TestSink probe, new elements will be emitted as requested. The Projection is stopped once the assert function completes.
sourceprojectionTestKit.runWithTestSink(projection){ sinkProbe =>
sinkProbe.request(1)
sinkProbe.expectNext(Done)}// confirm that cart checkout was inserted in db
cartViewRepository.findById("abc-def").futureValue
To test a handler in isolation you may want to mock out the implementation of a Projection or SourceProvider so that you don’t have to setup and teardown the associated technology as part of your integration test. For example, you may want to project against a Cassandra database, or read envelopes from an Akka Persistence journal source, but you don’t want to have to run Docker containers or embedded/in-memory services just to run your tests. The TestProjectionTestProjection allows you to isolate the runtime of your handler so that you don’t need to run these services. Using a TestProjection has the added benefit of being fast, since you can run everything within the JVM that runs your tests.
Alongside the TestProjection is the TestSourceProviderTestSourceProvider which can be used to provide test data to the TestProjection running the handler. Test data can be represented in an akka streams SourceSource that is passed to the TestSourceProvider constructor.