天天看點

Akka TestKit測試包的使用

testkit,測試類必須繼承幾個接口:

這些接口幫我們實作初始化actor testsystem,testactor,測試結束清理.

測試環境分為:

單線程

多線程

多jvm

這個環境下,我們一般是簡單的測試,使用testactorref允許我們直接通路silentactor内部

testactorref和localactorref,使用隻做測試用的callingthreaddispatcher作為排程器,callingthreaddispatcher不會在新線程中調用actor

由于不能直接通路actor,我們隻能通過testactor擷取actor的message,比如:

expectmsg,expectmsgpf方法接受來自test actor的消息。是以當要測試一個actor相應的message,可以将消息發送給testactor,testactor轉發到我們的測試方法。

discuss on chapter 5

我們歸類一下actor,有如下三種:

不傳回,不發送message。我們要測試它是否接受到了message,知否改變了内部狀态,是否抛出異常。

對于單線程環境,我們用testactorref就可以。多線程環境我們還是要用testactor。

會發送message給其他的actor,一般我們測試将其作為黑盒.使用testactor接受消息,并使用expectmsg或者expectmsgpf從testactor擷取從被測試actor收到的message。

接受一個message,并與一些object互動。當我們發送message給它時,要檢測object是變動

testkit提供了testactor,我們使用:

expectmsg

expectnomsg

ignoremsg

expectmsgpf

等方法來擷取testactor接受的message.如果我們要接受發送給多個actor的message,我們就要用testprobe了,直接調用<code>testprobe()</code>即可

大多數情況下,使用testactor測試是最友善的。