How do we test actors in Java?

The only thing I've seen so far is the publication of a TypedActor test example. I take it to where you can test UntypedActor, say Junit? Acca documents are improving every day, but I do not see testing. Is it really obvious and I just missed something?

+6
source share
3 answers

For testing using JUnit, you will need the tools provided by JUnit, the documents for testing Actor (Java equiv is UntypedActor) are here: http://akka.io/docs/akka/snapshot/scala/testing.html

+5
source

Perhaps at least with versions 1.3 and 2.0 and the akka-testkit library.

You do something like this to set up your actor:

@Before public void initActor() { actorSystem = ActorSystem.apply(); actorRef = TestActorRef.apply(new AbstractFunction0() { @Override public Pi.Worker apply() { return new Pi.Worker(); } }, actorSystem); } 

Then you can use the TestProbe class to test your actor (for version 1.3 it is slightly different):

 @Test public void calculatePiFor0() { TestProbe testProbe = TestProbe.apply(actorSystem); Pi.Work work = new Pi.Work(0, 0); actorRef.tell(work, testProbe.ref()); testProbe.expectMsgClass(Pi.Result.class); TestActor.Message message = testProbe.lastMessage(); Pi.Result resultMsg = (Pi.Result) message.msg(); assertEquals(0.0, resultMsg.getValue(), 0.0000000001); } 

The blog that I wrote about some of my impressions is more available: http://fhopf.blogspot.com/2012/03/testing-akka-actors-from-java.html

+1
source

You might be interested in a blog post that I wrote: Testing AKKA actors with Mockito and FEST-Reflect The example I use is based on JUnit, Mockito and FEST-Reflect. Let me know if this is useful to you.

+1
source

Source: https://habr.com/ru/post/887876/


All Articles