# How to test an EventHandler with EventSourcedBehaviorTestKit?

**URL:** <https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796>\
**Category:** Persistence / Event Sourcing\
**Created:** [July 3, 2024, 8:00am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796 "2024-07-03T08:00:14Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:00am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/1 "2024-07-03T08:00:14Z")

</div>

How to test an EventHaldler with EventSourcedBehaviorTestKit?

> This topic is based on a conversation between an anonymous user of Akka and our “Ask AI about Akka” bot in the documentation.

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:03am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/2 "2024-07-03T08:03:37Z")

</div>

Testing an `EventSourcedBehavior` with `EventSourcedBehaviorTestKit` involves creating an instance of `EventSourcedBehaviorTestKit` and running commands against it. The result of running a command contains the events emitted by the command and the new state after applying the events, which you can assert on.  
Here is an example of how to do this in Scala:

```auto
class AccountExampleDocSpec
    extends ScalaTestWithActorTestKit(EventSourcedBehaviorTestKit.config)
    with AnyWordSpecLike
    with BeforeAndAfterEach
    with LogCapturing {

  private val eventSourcedTestKit =
    EventSourcedBehaviorTestKit[AccountEntity.Command, AccountEntity.Event, AccountEntity.Account](
      system,
      AccountEntity("1", PersistenceId("Account", "1")))

  override protected def beforeEach(): Unit = {
    super.beforeEach()
    eventSourcedTestKit.clear()
  }

  "Account" must {

    "be created with zero balance" in {
      val result = eventSourcedTestKit.runCommand[StatusReply[Done]](AccountEntity.CreateAccount(_))
      result.reply shouldBe StatusReply.Ack
      result.event shouldBe AccountEntity.AccountCreated
      result.stateOfType[AccountEntity.OpenedAccount].balance shouldBe 0
    }

    "handle Withdraw" in {
      eventSourcedTestKit.runCommand[StatusReply[Done]](AccountEntity.CreateAccount(_))
      // ... more tests
    }
  }
}

```

And here is an example in Java:

```auto
@ClassRule
public static final TestKitJunitResource testKit =
    new TestKitJunitResource(EventSourcedBehaviorTestKit.config());

private EventSourcedBehaviorTestKit<
        AccountEntity.Command, AccountEntity.Event, AccountEntity.Account>
    eventSourcedTestKit =
        EventSourcedBehaviorTestKit.create(
            testKit.system(), AccountEntity.create("1", PersistenceId.of("Account", "1")));

@Before
public void beforeEach() {
  eventSourcedTestKit.clear();
}

@Test
public void createWithEmptyBalance() {
  CommandResultWithReply<
          AccountEntity.Command, AccountEntity.Event, AccountEntity.Account, StatusReply<Done>>
      result = eventSourcedTestKit.runCommand(AccountEntity.CreateAccount::new);
  assertEquals(StatusReply.ack(), result.reply());
  assertEquals(AccountEntity.AccountCreated.INSTANCE, result.event());
  assertEquals(BigDecimal.ZERO, result.stateOfType(AccountEntity.OpenedAccount.class).balance);
}

```

In both examples, the `EventSourcedBehaviorTestKit` is created with the `EventSourcedBehavior` under test. The `runCommand` method is used to send commands to the behavior, and the result is used to assert on the emitted events and the new state.  
For more details, you can refer to the [Akka documentation on unit testing with the ActorTestKit and EventSourcedBehaviorTestKit](https://doc.akka.io/docs/akka/current/typed/persistence-testing.html).

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:04am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/3 "2024-07-03T08:04:57Z")

</div>

How to test an EventHandler is executed after reading from journal in eventsourced actor?

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:08am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/4 "2024-07-03T08:08:23Z")

</div>

Testing that an `EventHandler` is executed after reading from the journal in an `EventSourcedBehavior` involves using the `EventSourcedBehaviorTestKit`. This test kit allows you to run commands against your behavior and assert on the events and state changes that result from those commands.

Here’s an example of how you might do this in Scala:

```auto
val eventSourcedTestKit =
  EventSourcedBehaviorTestKit[Command, Event, State](
    system,
    MyPersistentBehavior(PersistenceId.ofUniqueId("abc")))

val result = eventSourcedTestKit.runCommand(MyCommand)
result.event shouldBe MyEvent
result.stateOfType[MyState].myProperty shouldBe expectedValue

```

In this example, `MyPersistentBehavior` is the `EventSourcedBehavior` you’re testing, `MyCommand` is a command that will trigger an event, `MyEvent` is the expected event, and `MyState` is the expected state after the event has been handled.

In Java, the code would look like this:

```auto
EventSourcedBehaviorTestKit<Command, Event, State> eventSourcedTestKit =
  EventSourcedBehaviorTestKit.create(
    system,
    MyPersistentBehavior.create(PersistenceId.ofUniqueId("abc")));

CommandResultWithReply<Command, Event, State, Reply> result =
  eventSourcedTestKit.runCommand(reply -> new MyCommand(reply));
assertEquals(MyEvent.class, result.event().getClass());
assertEquals(expectedValue, result.stateOfType(MyState.class).getMyProperty());

```

In both examples, the `runCommand` method is used to send a command to the behavior. The result of this method includes the events emitted by the command and the new state after the events have been applied. You can then assert that the correct event was emitted and that the state has been updated correctly.  
For more details, you can refer to the [Akka documentation on unit testing with the ActorTestKit and EventSourcedBehaviorTestKit](https://doc.akka.io/docs/akka/current/typed/persistence-testing.html).

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:16am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/5 "2024-07-03T08:16:34Z")

</div>

How to manually send events to actor with EventSourcedBehaviorTestKit?

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:20am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/6 "2024-07-03T08:20:04Z")

</div>

The `EventSourcedBehaviorTestKit` does not directly support manually sending events to an actor. It is designed to test the behavior of an actor in response to commands, not direct event inputs.

When you run a command using `EventSourcedBehaviorTestKit.runCommand()`, it will return a result that contains the events emitted by the command and the new state after applying the events.

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:22am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/7 "2024-07-03T08:22:13Z")

</div>

How to test that EventSourcedBehavior actor replays events from journal?

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:25am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/8 "2024-07-03T08:25:25Z")

</div>

Testing that an `EventSourcedBehavior` actor replays events from the journal involves using the `EventSourcedBehaviorTestKit`. This test kit allows you to run commands against your behavior and assert on the events and state changes that result from those commands.

To test event replay, you can use the `restart` method of the `EventSourcedBehaviorTestKit`. This will restart the behavior, which will then recover from stored snapshot and events from previous commands.

Here’s an example of how you might do this in Scala:

```auto
val eventSourcedTestKit =
  EventSourcedBehaviorTestKit[Command, Event, State](
    system,
    MyPersistentBehavior(PersistenceId.ofUniqueId("abc")))

// Run a command that results in an event
val result = eventSourcedTestKit.runCommand(MyCommand)
result.event shouldBe MyEvent

// Restart the behavior
eventSourcedTestKit.restart()

// Run another command and assert that the state has been updated correctly
val resultAfterRestart = eventSourcedTestKit.runCommand(AnotherCommand)
resultAfterRestart.stateOfType[MyState].myProperty shouldBe expectedValue

```

In Java, the code would look like this:

```auto
EventSourcedBehaviorTestKit<Command, Event, State> eventSourcedTestKit =
  EventSourcedBehaviorTestKit.create(
    system,
    MyPersistentBehavior.create(PersistenceId.ofUniqueId("abc")));

// Run a command that results in an event
CommandResultWithReply<Command, Event, State, Reply> result =
  eventSourcedTestKit.runCommand(reply -> new MyCommand(reply));
assertEquals(MyEvent.class, result.event().getClass());

// Restart the behavior
eventSourcedTestKit.restart();

// Run another command and assert that the state has been updated correctly
CommandResultWithReply<Command, Event, State, Reply> resultAfterRestart =
  eventSourcedTestKit.runCommand(reply -> new AnotherCommand(reply));
assertEquals(expectedValue, resultAfterRestart.stateOfType(MyState.class).getMyProperty());

```

In both examples, the `runCommand` method is used to send a command to the behavior. The `restart` method is then used to simulate a restart of the actor, which will cause it to replay events from the journal. You can then run another command and assert that the state has been updated correctly, which indicates that the events were replayed correctly.

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:25am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/9 "2024-07-03T08:25:52Z")

</div>

In EventSourcedBehaviorTestKit how to create a probe to receive command responses?

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:28am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/10 "2024-07-03T08:28:04Z")

</div>

In `EventSourcedBehaviorTestKit`, you don’t need to create a probe to receive command responses. The `runCommand` method returns a `CommandResultWithReply` object that contains the reply to the command, the events that were emitted, and the new state of the actor.

Here’s an example in Scala:

```auto
val eventSourcedTestKit =
  EventSourcedBehaviorTestKit[Command, Event, State](
    system,
    MyPersistentBehavior(PersistenceId.ofUniqueId("abc")))

val result = eventSourcedTestKit.runCommand(MyCommand)
val reply = result.reply // This is the reply to the command

```

And in Java:

```auto
EventSourcedBehaviorTestKit<Command, Event, State> eventSourcedTestKit =
  EventSourcedBehaviorTestKit.create(
    system,
    MyPersistentBehavior.create(PersistenceId.ofUniqueId("abc")));

CommandResultWithReply<Command, Event, State, Reply> result =
  eventSourcedTestKit.runCommand(reply -> new MyCommand(reply));
Reply reply = result.reply(); // This is the reply to the command

```

In both examples, the `runCommand` method is used to send a command to the behavior. The result of this method includes the reply to the command, the events emitted by the command, and the new state after the events have been applied.

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:28am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/11 "2024-07-03T08:28:23Z")

</div>

What if the command includes an ActorRef to reply?

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:30am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/12 "2024-07-03T08:30:20Z")

</div>

If the command includes an `ActorRef` for the reply, you can use a `TestProbe` to receive the reply. A `TestProbe` is a special kind of actor used for testing, which allows you to assert on the messages it has received.

Here’s an example in Scala:

```auto
val eventSourcedTestKit =
  EventSourcedBehaviorTestKit[Command, Event, State](
    system,
    MyPersistentBehavior(PersistenceId.ofUniqueId("abc")))

val probe = testKit.createTestProbe[Reply]()
val result = eventSourcedTestKit.runCommand(replyTo => MyCommand(replyTo))

probe.expectMessage(MyExpectedReply)

```

And in Java:

```auto
EventSourcedBehaviorTestKit<Command, Event, State> eventSourcedTestKit =
  EventSourcedBehaviorTestKit.create(
    system,
    MyPersistentBehavior.create(PersistenceId.ofUniqueId("abc")));

TestProbe<Reply> probe = testKit.createTestProbe();
eventSourcedTestKit.runCommand(replyTo -> new MyCommand(replyTo));

probe.expectMessage(MyExpectedReply);

```

In both examples, a `TestProbe` is created to receive the reply. The `runCommand` method is then used to send a command to the behavior, with the `TestProbe`’s `ActorRef` passed in for the reply. The `expectMessage` method is then used on the `TestProbe` to assert that it received the expected reply.

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:30am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/13 "2024-07-03T08:30:42Z")

</div>

How to convert TestProbe to ActorRef?

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:32am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/14 "2024-07-03T08:32:11Z")

</div>

In Akka, you can get the `ActorRef` of a `TestProbe` using the `ref` method (in Scala) or the `getRef()` method (in Java).

Here’s an example in Scala:

```auto
val probe = testKit.createTestProbe[Reply]()
val actorRef: ActorRef[Reply] = probe.ref

```

And in Java:

```auto
TestProbe<Reply> probe = testKit.createTestProbe();
ActorRef<Reply> actorRef = probe.getRef();

```

In both examples, a `TestProbe` is created and its `ActorRef` is obtained. This `ActorRef` can then be used to send messages to the `TestProbe`.

For more details, you can refer to the [Akka documentation on testing](https://doc.akka.io/docs/akka/current/typed/testing-async.html).

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:33am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/15 "2024-07-03T08:33:07Z")

</div>

How to get ActorContext from EventSourcedBehaviorTestKit?

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:34am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/16 "2024-07-03T08:34:47Z")

</div>

The `EventSourcedBehaviorTestKi`t does not provide a direct way to access the `ActorContext`. The `ActorContext` is typically used within the `EventSourcedBehavior` itself, not in the test code.

If you need to use the `ActorContext` in your `EventSourcedBehavior`, you can obtain it by wrapping the behavior construction with `Behaviors.setup`. Here’s an example in Scala:

```auto
def apply(): Behavior[String] =
  Behaviors.setup { context =>
    EventSourcedBehavior[String, String, State](
      persistenceId = PersistenceId.ofUniqueId("myPersistenceId"),
      emptyState = State(),
      commandHandler = CommandHandler.command { cmd =>
        context.log.info("Got command {}", cmd)
        Effect.none
      },
      eventHandler = {
        case (state, _) => state
      })
  }

```

And in Java:

```auto
public static Behavior<Command> create(PersistenceId persistenceId) {
    return Behaviors.setup(ctx -> new MyPersistentBehavior(persistenceId, ctx));
}

private final ActorContext<Command> context;

public MyPersistentBehavior(PersistenceId persistenceId, ActorContext<Command> ctx) {
    super(persistenceId);
    this.context = ctx;
}

```

In both examples, `Behaviors.setup` is used to create the `EventSourcedBehavior`, and the `ActorContext` is passed into the constructor of the behavior.

For more details, you can refer to the [Akka documentation on accessing the ActorContext](https://doc.akka.io/docs/akka/current/typed/persistence.html#accessing-the-actorcontext).

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:35am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/17 "2024-07-03T08:35:27Z")

</div>

With EventSourcedBehaviorTestKit, can I test an actor with wrapped EventSourcedBehavior inside Behavior?

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:37am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/18 "2024-07-03T08:37:56Z")

</div>

Yes, you can test an actor with a wrapped `EventSourcedBehavior` using `EventSourcedBehaviorTestKit`. The test kit is designed to work with `EventSourcedBehavior,` whether it’s used directly or wrapped inside another behavior.

Here’s an example in Scala:

```auto
val eventSourcedTestKit =
  EventSourcedBehaviorTestKit[Command, Event, State](
    system,
    Behaviors.setup { context =>
      MyPersistentBehavior(PersistenceId.ofUniqueId("abc"), context)
    })

val result = eventSourcedTestKit.runCommand(MyCommand)

```

And in Java:

```auto
EventSourcedBehaviorTestKit<Command, Event, State> eventSourcedTestKit =
  EventSourcedBehaviorTestKit.create(
    system,
    Behaviors.setup(ctx -> MyPersistentBehavior.create(PersistenceId.ofUniqueId("abc"), ctx)));

CommandResultWithReply<Command, Event, State, Reply> result =
  eventSourcedTestKit.runCommand(reply -> new MyCommand(reply));

```

In both examples, `Behaviors.setup` is used to create the `EventSourcedBehavior`, and the `EventSourcedBehaviorTestKit` is used to test it.

For more details, you can refer to the [Akka documentation on unit testing with the ActorTestKit and EventSourcedBehaviorTestKit](https://doc.akka.io/docs/akka/current/typed/persistence-testing.html) and the [Akka documentation on wrapping EventSourcedBehavior](https://doc.akka.io/docs/akka/current/typed/persistence.html#accessing-the-actorcontext).

---

<div class="post-metadata">

**Author:** ![AnonymousUser](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@AnonymousUser](https://discuss.akka.io/u/AnonymousUser)\
**Post date:** [July 3, 2024, 8:42am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/19 "2024-07-03T08:42:17Z")

</div>

This code leds to the error message “Only EventSourcedBehavior, or nested EventSourcedBehavior allowed.”

---

<div class="post-metadata">

**Author:** ![Ask-AI-Bot](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@Ask-AI-Bot](https://discuss.akka.io/u/Ask-AI-Bot)\
**Post date:** [July 3, 2024, 8:44am UTC](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796/20 "2024-07-03T08:44:32Z")

</div>

The error message “Only EventSourcedBehavior, or nested EventSourcedBehavior allowed” indicates that the `EventSourcedBehaviorTestKit` is being used with a behavior that is not an `EventSourcedBehavior` or a behavior that wraps an `EventSourcedBehavior`.

The `EventSourcedBehaviorTestKit` is specifically designed to test `EventSourcedBehavior` actors. If you’re trying to test a different kind of actor, you should use the `ActorTestKit` instead.

If you’re trying to test an `EventSourcedBehavior` that is wrapped inside another behavior, make sure that the wrapping behavior is properly set up to create the `EventSourcedBehavior`. Here’s an example of how to do this in Scala:

```auto
def apply(): Behavior[Command] =
  Behaviors.setup { context =>
    EventSourcedBehavior[Command, Event, State](
      persistenceId = PersistenceId.ofUniqueId("abc"),
      emptyState = State(),
      commandHandler = (state, cmd) => throw new NotImplementedError("TODO: process the command & return an Effect"),
      eventHandler = (state, evt) => throw new NotImplementedError("TODO: process the event return the next state"))
  }

```

And in Java:

```auto
public static Behavior<Command> create(PersistenceId persistenceId) {
  return Behaviors.setup(ctx -> new MyPersistentBehavior(persistenceId, ctx));
}

private MyPersistentBehavior(PersistenceId persistenceId, ActorContext<Command> ctx) {
  super(persistenceId);
  // setup your EventSourcedBehavior here
}

```

In both examples, `Behaviors.setup` is used to create the `EventSourcedBehavior`.

[Next page](https://discuss.akka.io/t/how-to-test-an-eventhandler-with-eventsourcedbehaviortestkit/10796.md?page=2)
