使用Java中的FakeApplication播放2.0访问播放的默认Akka actor系统

时间:2012-08-25 20:03:31

标签: playframework-2.0 akka

我正在编写一个测试案例来测试akka演员。但是,我必须在假应用程序之外创建一个单独的Akka系统。有没有办法从FakeApplication获得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();
    }
}

3 个答案:

答案 0 :(得分:1)

您可以尝试使用静态方法Akka.system()来获取Akka actor系统。

但我不认为它适用于FakeApplication,您可能需要使用testServer() helper启动Play服务器:

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

答案 1 :(得分:0)

我刚刚在scala测试中做到了这一点。但是你必须在运行块内部初始化actor。我猜它在java中会起作用 - 但我还没有在java中测试它。我不需要实际启动Web服务器。

答案 2 :(得分: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();
    }
}