doc: Add page for testing of EventSourcedBehavior, #27899 (#27902)

This commit is contained in:
Patrik Nordwall 2019-10-07 16:51:25 +02:00 committed by GitHub
parent 4901c4eb24
commit 3926ffa954
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 249 additions and 2 deletions

View file

@ -0,0 +1,65 @@
# Testing
## Dependency
To use Akka Persistence and Actor TestKit, add the module to your project:
@@dependency[sbt,Maven,Gradle] {
group1=com.typesafe.akka
artifact1=akka-persistence-typed_$scala.binary_version$
version1=$akka.version$
group2=com.typesafe.akka
artifact2=akka-actor-testkit-typed_$scala.binary_version$
version2=$akka.version$
scope2=test
}
## Unit testing
Unit testing of `EventSourcedBehavior` can be done with the @ref:[ActorTestKit](testing-async.md)
in the same way as other behaviors.
@ref:[Synchronous behavior testing](testing-sync.md) for `EventSourcedBehavior` is not supported yet, but
tracked in @github[issue #26338](#23712).
You need to configure a journal, and the in-memory journal is sufficient for unit testing. To enable the
in-memory journal you need to pass the following configuration to the @scala[`ScalaTestWithActorTestKit`]@java[`TestKitJunitResource`].
Scala
: @@snip [AccountExampleDocSpec.scala](/akka-cluster-sharding-typed/src/test/scala/docs/akka/cluster/sharding/typed/AccountExampleDocSpec.scala) { #inmem-config }
Java
: @@snip [AccountExampleDocTest.java](/akka-cluster-sharding-typed/src/test/java/jdocs/akka/cluster/sharding/typed/AccountExampleDocTest.java) { #inmem-config }
Optionally you can also configure a snapshot store. To enable the file based snapshot store you need to pass the
following configuration to the @scala[`ScalaTestWithActorTestKit`]@java[`TestKitJunitResource`].
Scala
: @@snip [AccountExampleDocSpec.scala](/akka-cluster-sharding-typed/src/test/scala/docs/akka/cluster/sharding/typed/AccountExampleDocSpec.scala) { #snapshot-store-config }
Java
: @@snip [AccountExampleDocTest.java](/akka-cluster-sharding-typed/src/test/java/jdocs/akka/cluster/sharding/typed/AccountExampleDocTest.java) { #snapshot-store-config }
Then you can `spawn` the `EventSourcedBehavior` and verify the outcome of sending commands to the actor using
the facilities of the @ref:[ActorTestKit](testing-async.md).
A full test for the `AccountEntity`, which is shown in the @ref:[Style Guide](style-guide.md), may look like this:
Scala
: @@snip [AccountExampleDocSpec.scala](/akka-cluster-sharding-typed/src/test/scala/docs/akka/cluster/sharding/typed/AccountExampleDocSpec.scala) { #test }
Java
: @@snip [AccountExampleDocTest.java](/akka-cluster-sharding-typed/src/test/java/jdocs/akka/cluster/sharding/typed/AccountExampleDocTest.java) { #test }
Note that each test case is using a different `PersistenceId` to not interfere with each other.
## Integration testing
The in-memory journal and file based snapshot store can be used also for integration style testing of a single
`ActorSystem`, for example when using Cluster Sharding with a single Cluster node.
For tests that involve more than one Cluster node you have to use another journal and snapshot store.
While it's possible to use the @ref:[Persistence Plugin Proxy](../persistence-plugins.md#persistence-plugin-proxy)
it's often better and more realistic to use a real database.
See [akka-samples issue #128](https://github.com/akka/akka-samples/issues/128).

View file

@ -7,6 +7,7 @@ project.description: Event Sourcing with Akka Persistence enables actors to pers
* [Persistence coding style](persistence-style.md)
* [Persistence snapshotting](persistence-snapshot.md)
* [Persistence testing](persistence-testing.md)
* [Persistence schema evolution](../persistence-schema-evolution.md)
* [Persistence query](../persistence-query.md)
* [Persistence query LevelDB](../persistence-query-leveldb.md)
@ -23,7 +24,7 @@ For the Akka Classic documentation of this feature see @ref:[Classic Akka Persis
## Dependency
To use Akka Persistence Typed, add the module to your project:
To use Akka Persistence, add the module to your project:
@@dependency[sbt,Maven,Gradle] {
group=com.typesafe.akka

View file

@ -8,7 +8,7 @@ For the Akka Classic documentation of this feature see @ref:[Classic Testing](..
## Dependency
To use Akka TestKit add the module to your project:
To use Actor TestKit add the module to your project:
@@dependency[sbt,Maven,Gradle] {
group=com.typesafe.akka