通过 java 在 akka 中测试 actor 并有 ActorInitializationException
Test actor in akka by java and have ActorInitializationException
我想在创建演员样本 code.but 中使用来自演员测试 运行 演员测试抛出 ActorInitializationException。我的目标是通过 actor test 查看测试目标,我正在使用 junit test 但是
我的 class 代码在下面:
public class Worker extends AbstractActor {
private ActorSystem system = ActorSystem.create("worker");;
private Props props = Props.create(Worker.class , system.guardian().toString());
private ActorRef actor = system.actorOf(props);
@Override
public void preStart() {
System.out.println("worker actor started");
}
@Override
public void postStop() {
System.out.println("worker actor stopped");
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchAny(x -> getSender().tell(x, getSelf()))
.build();
}
@Test
public void testMain() throws Exception {
Future sFuture = ask(actor, "ping" , 1000);
System.out.println("First : " + sFuture);
}
}
and when running test method in up code this exception occur:
akka.actor.ActorInitializationException: You cannot create an instance of [akka.worker.Worker] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.
at akka.actor.ActorInitializationException$.apply(Actor.scala:192)
at akka.actor.Actor$class.$init$(Actor.scala:467)
at akka.actor.AbstractActor.<init>(AbstractActor.scala:132)
at ir.dotin.alm.akka.worker.Worker.<init>(Worker.java:17)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:187)
at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:236)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
我的猜测:您在 actor 内部编写了测试。 Junit 尝试用 new()
实例化包含测试的 class,因此失败了。尝试拆分定义:
public class Worker extends AbstractActor {
@Override
public void preStart() {
System.out.println("worker actor started");
}
@Override
public void postStop() {
System.out.println("worker actor stopped");
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchAny(x -> getSender().tell(x, getSelf()))
.build();
}
}
然后,在另一个文件中:
public class MyTest() {
private ActorSystem system = ActorSystem.create("worker");;
private Props props = Props.create(Worker.class , system.guardian().toString());
private ActorRef actor = system.actorOf(props);
@Test
public void testMain() throws Exception {
Future sFuture = ask(actor, "ping" , 1000);
System.out.println("First : " + sFuture);
}
}
我想在创建演员样本 code.but 中使用来自演员测试 运行 演员测试抛出 ActorInitializationException。我的目标是通过 actor test 查看测试目标,我正在使用 junit test 但是 我的 class 代码在下面:
public class Worker extends AbstractActor {
private ActorSystem system = ActorSystem.create("worker");;
private Props props = Props.create(Worker.class , system.guardian().toString());
private ActorRef actor = system.actorOf(props);
@Override
public void preStart() {
System.out.println("worker actor started");
}
@Override
public void postStop() {
System.out.println("worker actor stopped");
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchAny(x -> getSender().tell(x, getSelf()))
.build();
}
@Test
public void testMain() throws Exception {
Future sFuture = ask(actor, "ping" , 1000);
System.out.println("First : " + sFuture);
}
}
and when running test method in up code this exception occur:
akka.actor.ActorInitializationException: You cannot create an instance of [akka.worker.Worker] explicitly using the constructor (new). You have to use one of the 'actorOf' factory methods to create a new actor. See the documentation.
at akka.actor.ActorInitializationException$.apply(Actor.scala:192)
at akka.actor.Actor$class.$init$(Actor.scala:467)
at akka.actor.AbstractActor.<init>(AbstractActor.scala:132)
at ir.dotin.alm.akka.worker.Worker.<init>(Worker.java:17)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:187)
at org.junit.runners.BlockJUnit4ClassRunner.runReflectiveCall(BlockJUnit4ClassRunner.java:236)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:233)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access[=12=]0(ParentRunner.java:50)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:262)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:84)
我的猜测:您在 actor 内部编写了测试。 Junit 尝试用 new()
实例化包含测试的 class,因此失败了。尝试拆分定义:
public class Worker extends AbstractActor {
@Override
public void preStart() {
System.out.println("worker actor started");
}
@Override
public void postStop() {
System.out.println("worker actor stopped");
}
@Override
public Receive createReceive() {
return receiveBuilder()
.matchAny(x -> getSender().tell(x, getSelf()))
.build();
}
}
然后,在另一个文件中:
public class MyTest() {
private ActorSystem system = ActorSystem.create("worker");;
private Props props = Props.create(Worker.class , system.guardian().toString());
private ActorRef actor = system.actorOf(props);
@Test
public void testMain() throws Exception {
Future sFuture = ask(actor, "ping" , 1000);
System.out.println("First : " + sFuture);
}
}