title: Junit testing an Akka singleton actor: preStart() hook not called
body: <p>I would like to test a singleton actor using java in Scala IDE build of Eclipse SDK (Build id: 3.0.2-vfinal-20131028-1923-Typesafe) and Akka is 2.3.1.</p>
  
  <pre><code>public class WorkerTest {
  
  static ActorSystem system;
  
  @BeforeClass
  public static void setup() {
      system = ActorSystem.create("ClusterSystem");
  }
  
  @AfterClass
  public static void teardown() {
      JavaTestKit.shutdownActorSystem(system);
      system = null;
  }
  
  @Test
  public void testWorkers() throws Exception {
      new JavaTestKit(system) {{
  
          system.actorOf(ClusterSingletonManager.defaultProps(
                  Props.create(ClassSingleton.class), "class",
                  PoisonPill.getInstance(),"backend"), "classsingleton");
  
          ActorRef selection = system.actorOf(ClusterSingletonProxy.defaultProps("user/classsingleton/class", "backend"), "proxy");
          System.out.println(selection);
      }};
  }
  }
  </code></pre>
  
  <p>the ClassSingleton.java:</p>
  
  <pre><code>public class ClassSingleton extends UntypedActor {
  
  LoggingAdapter log = Logging.getLogger(getContext().system(), this);
  
  public ClassSingleton() {
      System.out.println("Constructor is done");
  }
  
  public static Props props() {
      return Props.create(ClassOperator.class);
  }
  
  @Override
  public void preStart() throws Exception {
      ActorRef selection = getSelf();
      System.out.println("ClassSingleton ActorRef...  " + selection);
  }   
  
  @Override
  public void onReceive(Object message) {
  }
  
  @Override
  public void postStop() throws Exception {
      System.out.println("postStop ...  ");
  }   
  
  
  }
  </code></pre>
  
  <p>The ClassSingleton actor is doing nothing, the printout is:
  Actor[akka://ClusterSystem/user/proxy#-893814405] only, which is printed from the ClusterSingletonProxy. No exception and Junit is done, green flag. In debugging ClassSingleton is not called (including contructor and preStart()). Sure it is me, but what is the mistake? Even more confusing that the same ClassSingleton ClusterSingletonManager code is working fine outside of javatestkit and junit.</p>
  
  <p>I suspect that the cluster setup might be reponsible, so I tried to include and exclude the following code (no effect). However I would like to understand why we need it, if we need it (it is from an example code).
  Many thanks for your help.</p>
  
  <pre><code>Address clusterAddress = Cluster.get(system).selfAddress();
  Cluster.get(system).join(clusterAddress);
  </code></pre>