Reputation: 9428
I am writing a test case to test akka actor. However, I have to create a separate Akka system outside of fake application. Is there a way to get akka actor system from FakeApplication?
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();
}
}
Upvotes: 0
Views: 1600
Reputation: 9428
This is what I did.
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();
}
}
Upvotes: 0
Reputation: 1954
I just did this in a scala test. But you have to init the actor inside of the running block. I am guessing it would work the same in java - but I haven't tested it in java. I didn't need to actually start a web server.
Upvotes: 0
Reputation: 21564
You could try to get the Akka actor system using the static method Akka.system()
.
But I don't think it works with a FakeApplication, you may need to start your Play server using the testServer() helper:
@Test
public void testInServer() {
running(testServer(3333), new Runnable() {
public void run() {
ActorSystem actorSystem = Akka.system();
// do whatever you need
}
});
}
Upvotes: 1