title: How to test Akka Actor functionality by mocking one or more methods in it
body: <p>I'm interested to know about how to test Akka Actor functionality, <strong>by mocking some methods</strong> (<em>substitute real object's/actor's method implementation by mocked one</em>) in Actor.</p>
  
  <p>I use <code>akka.testkit.TestActorRef</code>;</p>
  
  <p>Also: I tried to use <code>SpyingProducer</code> but it is not clear how to use it. (like I if I created actor inside its implementation it would be the same I have now).
  The google search result about that is not very <a href="https://www.google.com/search?q=SpyingProducer&amp;sugexp=chrome,mod=15&amp;sourceid=chrome&amp;ie=UTF-8#sclient=psy-ab&amp;q=SpyingProducer%20akka&amp;oq=SpyingProducer%20akka&amp;gs_l=serp.3...2684.3580.4.3806.5.1.4.0.0.0.212.212.2-1.1.0.epsugrccggmnoe..0.0...1.1.14.psy-ab.VAaiiJogNy0&amp;pbx=1&amp;bav=on.2,or.r_cp.r_qf.&amp;bvm=bv.46865395,d.cGE&amp;fp=9e0125f66928f75e&amp;biw=1536&amp;bih=754">verbose</a>.</p>
  
  <p>I use <code>powemockito</code> and <code>java</code>. But It does not matter. I would be interested to know <code>how to do it in principle</code> <strong>with any language with any framework</strong> </p>
  
  <blockquote>
    <p>(so if you do not know how power/mockito works just provide your
    code.. (please) or complete idea about how you would do it with your tools you know.)</p>
  </blockquote>
  
  <p>So, let's say we have an Actor to test:</p>
  
  <pre><code>package example.formock;
  
  import akka.actor.UntypedActor;
  
  public class ToBeTestedActor extends UntypedActor {
  
      @Override
      public void onReceive(Object message) throws Exception {
  
          if (message instanceof String) {
              getSender().tell( getHelloMessage((String) message), getSelf());
          }
  
      }
  
      String getHelloMessage(String initMessage) { // this was created for test purposes (for testing mocking/spy capabilities). Look at the test
          return "Hello, " + initMessage;
      }
  
  }
  </code></pre>
  
  <p>And in our test we want to substitute <code>getHelloMessage()</code> returning something else.</p>
  
  <p>This is my try:</p>
  
  <pre><code>package example.formock;
  
  import akka.testkit.TestActorRef;
  ...
  
  @RunWith(PowerMockRunner.class)
  @PrepareForTest(ToBeTestedActor.class)
  public class ToBeTestedActorTest {
  
      static final Timeout timeout = new Timeout(Duration.create(5, "seconds"));
  
      @Test
      public void getHelloMessage() {
  
          final ActorSystem system = ActorSystem.create("system");
  
          // given
          final TestActorRef&lt;ToBeTestedActor&gt; actorRef = TestActorRef.create(
                  system,
                  Props.create(ToBeTestedActor.class),
                  "toBeTestedActor");
  
          // First try:
          ToBeTestedActor actorSpy = PowerMockito.spy(actorRef.underlyingActor());
          // change functionality
          PowerMockito.when(actorSpy.getHelloMessage (anyString())).thenReturn("nothing"); // &lt;- expecting result   
  
  
          try {
  
             // when
             Future&lt;Object&gt; future = Patterns.ask(actorRef, "Bob", timeout);
             // then
             assertTrue(future.isCompleted());
  
              // when
             String resultMessage = (String) Await.result(future, Duration.Zero());
              // then
             assertEquals("nothing", resultMessage);  // FAIL HERE
  
          } catch (Exception e) {
             fail("ops");
          }
      }
  }
  </code></pre>
  
  <p><strong>Result:</strong></p>
  
  <pre><code>org.junit.ComparisonFailure: 
  Expected :nothing
  Actual   :Hello, Bob
  </code></pre>