本文介绍了如何在Akka演员中测试公共方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个闲话演员:

class MyActor extends Actor {
  def recieve { ... }

  def getCount(id: String): Int = {
    //do a lot of stuff
    proccess(id)
    //do more stuff and return
  }
}

我正在尝试为getCount方法创建单元测试:

I am trying to create a unit test for the getCount method:

it should "count" in {
    val system = ActorSystem("Test")
    val myActor = system.actorOf(Props(classOf[MyActor]), "MyActor")

    myActor.asInstanceOf[MyActor].getCount("1522021") should be >= (28000)
}

但它不起作用:

  java.lang.ClassCastException: akka.actor.RepointableActorRef cannot be cast to com.playax.MyActor

如何测试此方法?

推荐答案

执行以下操作:

import org.scalatest._
import akka.actor.ActorSystem
import akka.testkit.TestActorRef
import akka.testkit.TestKit

class YourTestClassTest extends TestKit(ActorSystem("Testsystem")) with FlatSpecLike with Matchers {

  it should "count plays" in {
    val actorRef = TestActorRef(new MyActor)
    val actor = actorRef.underlyingActor
    actor.getCount("1522021") should be >= (28000)
  }
}

这篇关于如何在Akka演员中测试公共方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:24