Multiple Extension implementations

How can I have multiple implementations of the same extension, for example for testing purposes?

Say, for example, I have an extension for a database connection. Normally, that extension would use service discovery to find database servers. But now in an automatized test, I want the extension to connect to a local docker container.

I haven’t found a way to either pass arguments to an extension upon creation, nor “inject” a different implementation of the same Extension.

The only way I can imagine to do it is through configuration variables, but I’d like to avoid that if possible, because in my case it would mean some coupling of test and production code.

It’s right that extensions only have access to the ActorSystem, and it’s configuration when they are created.

If these are your own extensions you can define an interface that the extension implements, and then pass an instance of that to your actors where it is used instead of looking up the extension. In that way you can have different implementations for test and prod.

1 Like

Thanks a lot for the response and giving me a clear picture. In that case I might do it among the following lines instead:

object DatabaseSessionExtension extends ExtensionId[DatabaseSessionExtension] with ExtensionIdProvider {
  // Test-overridable
  private[database] var factory: () => DatabaseSessionExtension = () => new DatabaseSessionExtension()

  override def createExtension(system: ExtendedActorSystem): DatabaseSessionExtension = factory()
// ...