Spying with mockito on an akk actor

I would like to follow the instance of my actor, but you can’t just create it using the new keyword. I understood the following solution:

val testActorSpy = spy(TestActorRef(new TestActor).underlyingActor) val testActorRef = TestActorRef(testActorSpy ) 

but in this way I create one unnecessary actor. Are there any cleaner solutions?

+6
source share
1 answer

So, my understanding of the Akka-Actor system is that you have to do this through properties, and then right?

Thus, create an actor through the props, and when testing, simply return the spy to the actor.

so this should give you the result:

 val testActorRef = TestActorRef(spy(new TestActor)) val testActorSpy = testActorRef.underlyingActor 

Keep in mind that the primary agent is destroyed when the actor restarts. therefore, mockery of this may not be the best option.

If you use the actor directly through the system, you can also test and bypass the stream base system.

See this (paste the code below for java).

 static class MyActor extends UntypedActor { public void onReceive(Object o) throws Exception { if (o.equals("say42")) { getSender().tell(42, getSelf()); } else if (o instanceof Exception) { throw (Exception) o; } } public boolean testMe() { return true; } } @Test public void demonstrateTestActorRef() { final Props props = Props.create(MyActor.class); final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "testA"); final MyActor actor = ref.underlyingActor(); assertTrue(actor.testMe()); } 
0
source

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


All Articles