在类的参数[]中找不到匹配的构造函数

时间:2016-12-07 11:35:59

标签: scala akka

我有以下测试:

  it should "ask yarn about the running jobs" in new TestScope {
    val testSender: TestProbe = TestProbe()
    val testReceiver: TestProbe = TestProbe()
    val yarnActorRef = system.actorOf(YarnActor.props(testReceiver.ref))

    testSender.send(yarnActorRef, UpdateListOfJobs(Instant.now()))
    testReceiver.expectMsg(YarnJobStatus("Not running"))
  }

对于这个演员:

object YarnActor {
  trait Message

  case class UpdateListOfJobs(timeStamp: Instant) extends Message

  def props(stateActorRef: ActorRef) = Props(new YarnActor(stateActorRef))
}

class YarnActor(stateActorRef: ActorRef) extends Actor with ActorLogging {
  override def receive: Receive = {
    case UpdateListOfJobs(timeStamp) => {
      //check if the job is still running
      val address = url("someUrlAddress")
      val status: Future[String] = Await.ready(Http(address OK as.String), 1 second)

      status onComplete {
        case Success(message) => stateActorRef ! YarnJobStatus(message)
        case Failure(_) => stateActorRef ! YarnJobStatus("Not running")
      }
    }
  }
}

我得到了:

[info] - should ask yarn about the running jobs *** FAILED ***
[info]   java.lang.IllegalArgumentException: no matching constructor found on class myPackage.YarnActor for arguments []

为什么会出现此错误? 在伴侣对象中,我用1个参数构建我的actor(应该只需要它)。

1 个答案:

答案 0 :(得分:0)

我没有使用spec2,但像这样的代码正在使用scalatest

import akka.actor._
import akka.testkit.{ImplicitSender, TestKit, TestProbe}
import dispatch._
import org.joda.time.Instant
import org.scalatest.{FlatSpecLike, Matchers}

import scala.concurrent.duration._
import scala.concurrent.{Await, Future}
import scala.util.{Failure, Success}


case class YarnJobStatus(message: String)
object YarnActor {
  trait Message

  case class UpdateListOfJobs(timeStamp: Instant) extends Message

  def props(stateActorRef: ActorRef) =
    Props(new YarnActor(stateActorRef))
}

class YarnActor(stateActorRef: ActorRef)
  extends Actor with ActorLogging {
  import Defaults._
  import YarnActor._

  override def receive: Receive = {
    case UpdateListOfJobs(timeStamp) => {
      //check if the job is still running
      val addr = url("http://127.0.0.1")

      val status: Future[String] = Await.ready(Http(addr OK as.String), 2 second)

      status onComplete {
        case Success(message) => stateActorRef ! YarnJobStatus(message)
        case Failure(_) => stateActorRef ! YarnJobStatus("Not running")
      }
    }
  }
}

class ActorSpec extends TestKit(ActorSystem("MySpec"))
  with ImplicitSender with FlatSpecLike with Matchers {

  it should "ask yarn about the running jobs" in {
      val testSender: TestProbe = TestProbe()
      val testReceiver: TestProbe = TestProbe()
      val yarnActorRef = system.actorOf(YarnActor.props(testReceiver.ref))

      testSender.send(yarnActorRef, YarnActor.UpdateListOfJobs(Instant.now()))
      testReceiver.expectMsg[YarnJobStatus](YarnJobStatus("Not running"))
    }
}