Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code is already properly testable:</p> <ul> <li>you can test your business logic separately, since you can just instantiate your <code>Helper</code> outside of the actor</li> <li>once you are sure that the <code>Helper</code> does what it is supposed to do, just send some inputs to the actor and observe that the right replies come back</li> </ul> <p>Now if you need to have a “mocked” <code>Worker</code> to test some other component, just don’t use a Worker at all, use a <code>TestProbe</code> instead. Where you would normally get the <code>ActorRef</code> of the Worker, just inject <code>probe.getRef()</code>.</p> <p>So, how to inject that?</p> <p>I’ll assume that your other component is an Actor (because otherwise you won’t have trouble applying whatever injection technique you normally use). Then there are three basic choices:</p> <ol> <li>pass it in as constructor argument</li> <li>send it within a message</li> <li>if the actor creates the ref as its child, pass in the <code>Props</code>, possibly in an alternative constructor</li> </ol> <p>The third case is probably what you are looking at (I’m guessing based on the actor class’ name):</p> <pre><code>public class MyParent extends UntypedActor { final Props workerProps; public MyParent() { workerProps = new Props(...); } public MyParent(Props p) { workerProps = p; } ... getContext().actorOf(workerProps, "worker"); } </code></pre> <p>And then you can inject a <code>TestProbe</code> like this:</p> <pre><code>final TestProbe probe = new TestProbe(system); final Props workerMock = new Props(new UntypedActorFactory() { public UntypedActor create() { return new UntypedActor() { @Override public void onReceive(Object msg) { probe.getRef().tell(msg, getSender()); } }; } }); final ActorRef parent = system.actorOf(new Props(new UntypedActorFactory() { public UntypedActor create() { return new MyParent(workerMock); } }), "parent"); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload