2012-08-25 74 views
0

我正在編寫測試用例來測試akka actor。但是,我必須在假應用程序之外創建一個單獨的Akka系統。有沒有辦法從FakeApplication獲得akka actor系統?從Java中的FakeApplication播放2.0訪問播放的默認Akka actor系統

public class ChannelWorkerTest { 

    private TestActorRef<ChannelWorker> actorRef; 
    private ActorSystem actorSystem; 

    @Before 
    public void initActor() { 
     actorSystem = ActorSystem.apply(); 
     actorRef = TestActorRef.apply(new Props(ChannelWorker.class), actorSystem); 
    } 


    @Test 
    public void calculatePiFor1() { 
     running(fakeApplication(TestConf.getConf()), new Runnable() { 
      public void run() { 
       TestProbe testProbe = TestProbe.apply(actorSystem); 
       ..... 
       actorRef.tell(aMessage, testProbe.ref()); 
       } 
     }); 
    } 

    @After 
    public void shutdownActorSystem() { 
     actorSystem.shutdown(); 
    } 
} 

回答

0

這就是我所做的。

public class MyHelpers extends Helpers { 

    /** 
    * Build a new fake application. 
    */ 
    public static MyFakeApplication myFakeApplication(Map<String,String> additionalConfiguration, 
     List<String> withoutPlugins, List<String> additionalPlugin) { 
     return new MyFakeApplication(new java.io.File("."), MyHelpers.class.getClassLoader(), 
      additionalConfiguration, withoutPlugins, additionalPlugin); 
    } 

    /** 
    * Executes a block of code in a running application. 
    */ 
    public static void myRunning(MyFakeApplication fakeApplication, final Runnable block) { 
    try { 
     myStart(fakeApplication); 
     block.run(); 
    } finally { 
     myStop(fakeApplication); 
      play.core.Invoker$.MODULE$.system().shutdown(); 
      play.core.Invoker$.MODULE$.uninit(); 
    } 
    } 
} 


public class ChannelWorkerTest { 
    private MyFakeApplication app; 
    private TestActorRef<ChannelWorker> actorRef; 
    private ActorSystem actorSystem; 

    @Before 
    public void initTest() { 
    app = myFakeApplication(TestConf.getConf(), withoutPlugins, additionalPlugins); 
     actorSystem = play.api.libs.concurrent.Akka.system(app.getWrappedApplication()); 
     actorRef = TestActorRef.apply(new Props(new UntypedActorFactory() { 
      public UntypedActor create() { 
       return new MessageMaster(Config.NUMBER_OF_WORKER_ACTOR); 
      } 
     }), actorSystem); 
    } 


    @Test 
    public void calculatePiFor1() { 
     running(fakeApplication(TestConf.getConf()), new Runnable() { 
      public void run() { 
       TestProbe testProbe = TestProbe.apply(actorSystem); 
       actorRef.tell("Hello", testProbe.ref()); 
       testProbe.expectNoMsg(Duration.apply(100, TimeUnit.MILLISECONDS)); 
       } 
     }); 
    } 

    @After 
    public void shutdownActorSystem() { 
     actorSystem.shutdown(); 
    } 
} 
1

你可以嘗試使用靜態方法Akka.system()得到Akka actor系統。

但我不認爲它的工作原理與FakeApplication,您可能需要使用testServer() helper開始你的遊戲服務器:

@Test 
public void testInServer() { 
    running(testServer(3333), new Runnable() { 
     public void run() { 
     ActorSystem actorSystem = Akka.system(); 
     // do whatever you need 
     } 
    }); 
} 
0

我只是在一個階測試這樣做。但是你必須在運行塊內部初始化actor。我猜測它會在java中工作 - 但我沒有在java中測試過它。我並不需要真正啓動一個Web服務器。