Java TestProbe with ReceiveWhile

31 views
Skip to first unread message

Allan Brighton

unread,
Dec 6, 2016, 1:10:34 PM12/6/16
to Akka User List
In the Akka Scala API you can call receiveWhile to receive messages sent to a TestProbe:

val msgs = testProbe.receiveWhile(5.seconds) {
case x
=> ...
} 

In the Java API you can do something similar, but I could not find a way to use ReceiveWhile with a TestProbe:

final CurrentState[] msgs =
new ReceiveWhile<CurrentState>(CurrentState.class, duration("5 seconds")) {
protected CurrentState match(Object in) {
if (in instanceof CurrentState) {
CurrentState cs = (CurrentState) in;
        // ...
return cs;
}
throw noMatch();
}
}.get(); // this extracts the received messages

Is there a way to do this in the Java API where the messages are sent to a TestProbe?


Luis Pedrosa

unread,
Dec 7, 2016, 10:10:24 AM12/7/16
to Akka User List
Looking at the github repo, there is a receive while usage example here.

I believe you can consider the JavaTestKit class to be your test probe. You can instantiate it and assign it to a variable, which you can then call getRef() in order to send messages to it later on.

From the example, it seems that you need to setup your receive while conditions in a double brace initialization block. So you need to bear this in mind before initializing the test kit.

I hope this helps!
Reply all
Reply to author
Forward
0 new messages