1
votes

I am using TestKit to test some of my classes for a Scala project I am working on involving Akka Actors, and I'm running into this issue:

One or more requested classes are not Suites: poc.PocConstituentsWatcher

The class in question looks like this:

class PocConstituentsWatcher(_system: ActorSystem) extends TestKit(_system) with ImplicitSender with WordSpecLike with Matchers with BeforeAndAfter with BeforeAndAfterAll {

I didn't used to have this issue, because I had

def this() = this(ActorSystem)

but now I define my own ActorSystem via injection, so I have val actorSystem = injector.instance[ActorSystem] instead, and when I do

def this() = this(actorSystem)

I get an error saying it can't find actorSystem. I think it's because the constructor signature is incorrect? Thanks for your help.

EDIT:

Showed how to inject my actor system.

val injector = Guice.createInjector(new AkkaModule)
val actorSystem = injector.instance[ActorSystem]

In AkkaModule

object AkkaModule {
   class ActorSystemProvider @Inject() (val config: Config, val injector: Injector) extends Provider[ActorSystem] {
override def get() = {
  val system = ActorSystem("poc-actor-system", config)
  GuiceAkkaExtension(system).initialize(injector)

  system
}
  }
}

 class AkkaModule extends AbstractModule with ScalaModule {

  override def configure() {
    bind[ActorSystem].toProvider[ActorSystemProvider].asEagerSingleton()
  }
}
1
ScalaTest and Specs2 require that your class have a default constructor (which is what you added with def this() = this(ActorSystem). Off the cuff I can't think of a way of getting around this. - Mario Camou
Hmm... So should I just include def this() = this(ActorSystem)? Will that cause me any problems? What is the default constructor used for? Alternatively I just redefined the method signature and it seemed to work... - jstnchng
You mention that you are using injection to define your ActorSystem. How are you injecting it? Do you have an example? - Mario Camou
Edited to add an example. - jstnchng
Is this a compile-time or a runtime error? - Mario Camou

1 Answers

0
votes

I ended up using

def this() = this(ActorSystem("poc-actor-system")) which worked but is slightly worrying since technically the default constructor doesn't use the same actor system as the one I created val actorSystem = injector.instance[ActorSystem]. Will report back if anything breaks in the future...