When testing Akka actor I receive the error: no matching constructor found on MyActor

304 views Asked by At

I am creating a test for Akka actors and for some reason, it does not let me create an actor. It throws a message saying no matching constructor found on class SimpleActor. It seemed to be a bug on Akka version 2.4.x based on this issue but I am using a newer version. I am using the following build.sbt:

name := """explore-akka"""
version := "1.1"
scalaVersion := "2.12.7"
val akkaVersion = "2.6.10"
val scalaBinVersion = "2.12"
val scalaTestVersion = "3.2.2"

libraryDependencies ++= Seq(
  "com.typesafe.akka" %% "akka-actor" % akkaVersion,
  "com.typesafe.akka" %% "akka-testkit" % akkaVersion,
  "org.scalatest" %% "scalatest" % scalaTestVersion
)

mainClass in Compile := Some("org.github.felipegutierrez.explore.akka.MainClass")

enablePlugins(JavaAppPackaging)
enablePlugins(DockerPlugin)
dockerUsername := Some("felipeogutierrez")

and the following test class in Scala:

package org.github.felipegutierrez.explore.akka.tests

import akka.actor.{Actor, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestKit}
import org.scalatest.BeforeAndAfterAll
import org.scalatest.wordspec.AnyWordSpecLike

class BasicSpec extends TestKit(ActorSystem())
  with ImplicitSender with AnyWordSpecLike with BeforeAndAfterAll {

  override def afterAll(): Unit = {
    TestKit.shutdownActorSystem(system)
  }
  import BasicSpec._
  "a simple actor" should {
    "send back the same message" in {
      val echoActor = system.actorOf(Props[SimpleActor])
      val message = "hello, test"
      echoActor ! message

      expectMsg(message)
    }
  }
  object BasicSpec {
    class SimpleActor extends Actor {
      override def receive: Receive = {
        case message => sender() ! message
      }
    }

  }
}

I also tried val echoActor = system.actorOf(Props[SimpleActor], name = "SimpleActor") and val echoActor = TestActorRef(Props[SimpleActor]) but I keep receiving this error:

no matching constructor found on class org.github.felipegutierrez.explore.akka.tests.BasicSpec$BasicSpec$SimpleActor for arguments []
java.lang.IllegalArgumentException: no matching constructor found on class org.github.felipegutierrez.explore.akka.tests.BasicSpec$BasicSpec$SimpleActor for arguments []
    at akka.util.Reflect$.error$1(Reflect.scala:89)
    at akka.util.Reflect$.findConstructor(Reflect.scala:113)
    at akka.actor.NoArgsReflectConstructor.<init>(IndirectActorProducer.scala:108)
    at akka.actor.IndirectActorProducer$.apply(IndirectActorProducer.scala:63)
    at akka.actor.Props.producer(Props.scala:131)
    at akka.actor.Props.<init>(Props.scala:144)
    at akka.actor.Props$.apply(Props.scala:62)
    at org.github.felipegutierrez.explore.akka.tests.BasicSpec.$anonfun$new$2(BasicSpec.scala:20)
    at org.scalatest.OutcomeOf.outcomeOf(OutcomeOf.scala:85)
    at org.scalatest.OutcomeOf.outcomeOf$(OutcomeOf.scala:83)
    at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
    at org.scalatest.Transformer.apply(Transformer.scala:22)
    at org.scalatest.Transformer.apply(Transformer.scala:20)
    at org.scalatest.wordspec.AnyWordSpecLike$$anon$3.apply(AnyWordSpecLike.scala:1076)
    at org.scalatest.TestSuite.withFixture(TestSuite.scala:196)
    at org.scalatest.TestSuite.withFixture$(TestSuite.scala:195)
    at org.github.felipegutierrez.explore.akka.tests.BasicSpec.withFixture(BasicSpec.scala:8)
    at org.scalatest.wordspec.AnyWordSpecLike.invokeWithFixture$1(AnyWordSpecLike.scala:1074)
    at org.scalatest.wordspec.AnyWordSpecLike.$anonfun$runTest$1(AnyWordSpecLike.scala:1086)
    at org.scalatest.SuperEngine.runTestImpl(Engine.scala:306)
    at org.scalatest.wordspec.AnyWordSpecLike.runTest(AnyWordSpecLike.scala:1086)
    at org.scalatest.wordspec.AnyWordSpecLike.runTest$(AnyWordSpecLike.scala:1068)
    at org.github.felipegutierrez.explore.akka.tests.BasicSpec.runTest(BasicSpec.scala:8)
    at org.scalatest.wordspec.AnyWordSpecLike.$anonfun$runTests$1(AnyWordSpecLike.scala:1145)
    at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:413)
    at scala.collection.immutable.List.foreach(List.scala:388)
    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401)
    at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:390)
    at org.scalatest.SuperEngine.$anonfun$runTestsInBranch$1(Engine.scala:427)
    at scala.collection.immutable.List.foreach(List.scala:388)
    at org.scalatest.SuperEngine.traverseSubNodes$1(Engine.scala:401)
    at org.scalatest.SuperEngine.runTestsInBranch(Engine.scala:396)
    at org.scalatest.SuperEngine.runTestsImpl(Engine.scala:475)
    at org.scalatest.wordspec.AnyWordSpecLike.runTests(AnyWordSpecLike.scala:1145)
    at org.scalatest.wordspec.AnyWordSpecLike.runTests$(AnyWordSpecLike.scala:1144)
    at org.github.felipegutierrez.explore.akka.tests.BasicSpec.runTests(BasicSpec.scala:8)
    at org.scalatest.Suite.run(Suite.scala:1112)
    at org.scalatest.Suite.run$(Suite.scala:1094)
    at org.github.felipegutierrez.explore.akka.tests.BasicSpec.org$scalatest$wordspec$AnyWordSpecLike$$super$run(BasicSpec.scala:8)
    at org.scalatest.wordspec.AnyWordSpecLike.$anonfun$run$1(AnyWordSpecLike.scala:1190)
    at org.scalatest.SuperEngine.runImpl(Engine.scala:535)
    at org.scalatest.wordspec.AnyWordSpecLike.run(AnyWordSpecLike.scala:1190)
    at org.scalatest.wordspec.AnyWordSpecLike.run$(AnyWordSpecLike.scala:1188)
    at org.github.felipegutierrez.explore.akka.tests.BasicSpec.org$scalatest$BeforeAndAfterAll$$super$run(BasicSpec.scala:8)
    at org.scalatest.BeforeAndAfterAll.liftedTree1$1(BeforeAndAfterAll.scala:213)
    at org.scalatest.BeforeAndAfterAll.run(BeforeAndAfterAll.scala:210)
    at org.scalatest.BeforeAndAfterAll.run$(BeforeAndAfterAll.scala:208)
    at org.github.felipegutierrez.explore.akka.tests.BasicSpec.run(BasicSpec.scala:8)
    at org.scalatest.tools.SuiteRunner.run(SuiteRunner.scala:45)
    at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13(Runner.scala:1320)
    at org.scalatest.tools.Runner$.$anonfun$doRunRunRunDaDoRunRun$13$adapted(Runner.scala:1314)
    at scala.collection.immutable.List.foreach(List.scala:388)
    at org.scalatest.tools.Runner$.doRunRunRunDaDoRunRun(Runner.scala:1314)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24(Runner.scala:993)
    at org.scalatest.tools.Runner$.$anonfun$runOptionallyWithPassFailReporter$24$adapted(Runner.scala:971)
    at org.scalatest.tools.Runner$.withClassLoaderAndDispatchReporter(Runner.scala:1480)
    at org.scalatest.tools.Runner$.runOptionallyWithPassFailReporter(Runner.scala:971)
    at org.scalatest.tools.Runner$.run(Runner.scala:798)
    at org.scalatest.tools.Runner.run(Runner.scala)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.runScalaTest2or3(ScalaTestRunner.java:40)
    at org.jetbrains.plugins.scala.testingSupport.scalaTest.ScalaTestRunner.main(ScalaTestRunner.java:27)

UPDATE: more stranger is that when I use the following test from the official doc it works! But when I copy the same code from the TestActors.echoActorProps to my SimpleActor it does not work.

  "An Echo actor" must {
    "send back messages unchanged" in {
      val echo = system.actorOf(TestActors.echoActorProps)
      echo ! "hello world"
      expectMsg("hello world")
    }
  }
1

There are 1 answers

0
Felipe On BEST ANSWER

I changed the implementation for val propsSimpleActor = {Props(new SimpleActor)} and it worked. I am not sure why even if I implemented the same code from the official documentation it was not working. The complete code is as follows:

package org.github.felipegutierrez.explore.akka.tests

import akka.actor.{Actor, ActorSystem, Props}
import akka.testkit.{ImplicitSender, TestActors, TestKit}
import org.scalatest.BeforeAndAfterAll
import org.scalatest.matchers.should.Matchers
import org.scalatest.wordspec.AnyWordSpecLike

import scala.concurrent.duration.DurationInt
import scala.util.Random

class BasicSpec extends TestKit(ActorSystem("BasicSpec"))
  with ImplicitSender
  with AnyWordSpecLike
  with Matchers
  with BeforeAndAfterAll {

  override def afterAll(): Unit = { TestKit.shutdownActorSystem(system) }

  "A simple actor" should {
    "send back the same message" in {
      val echoActor = system.actorOf(BasicSpec.propsSimpleActor)
      val message = "hello, test"
      echoActor ! message
      expectMsg(message)
    }
  }

  object BasicSpec {
    class SimpleActor extends Actor {
      override def receive = {
        case message => sender() ! message
      }
    }
    val propsSimpleActor = {Props(new SimpleActor)}
  }
}