Entity unit testing with PersistentEntityRegistry

I am new to lagom and I currently try to write a unit test for one of my PersistentEntity.

The constructor looks like this:
class EventEntity(persistentEntityRegistry: PersistentEntityRegistry)(implicit ex: ExecutionContext)

I am using the PersistentEntityRegistry in one of my command handlers to initialize/create another entity.

My questions:

  1. Is this the correct/idiomatic way to create new entities? So, is it ok to create one entity from within another? or should I better do this in the ServiceImpl layer after I received the reply from the command?
  2. If this is the way to go, how would I unit test this?

I’d recommend creating the entity from the service, not from another entity command handler.

Command handlers are synchronous, so you wouldn’t have any way to know whether creating the second entity succeeds, unless you block in the command handler, which can create many other problems. In general, persistent entity command handlers should be deterministic and self-contained: they only validate the command against the current state and decide what events to emit.

1 Like