0

我正在编写一个测试用例来测试 akka 演员。但是,我必须在假应用程序之外创建一个单独的 Akka 系统。有没有办法从 FakeApplication 获取 akka 演员系统?

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();
    }
}
4

3 回答 3

1

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

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

@Test
public void testInServer() {
  running(testServer(3333), new Runnable() {
      public void run() {
         ActorSystem  actorSystem = Akka.system();
         // do whatever you need
      }
  });
}
于 2012-08-26T14:35:28.273 回答
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();
    }
}
于 2012-12-20T21:49:52.903 回答
0

我只是在 scala 测试中做到了这一点。但是你必须在运行块内初始化actor。我猜它在 java 中的工作原理是一样的——但我还没有在 java 中测试过它。我不需要实际启动 Web 服务器。

于 2012-12-20T16:54:13.760 回答