传递FSM actor的ActorRef以初始化另一个actor

时间:2018-03-28 15:23:46

标签: java constructor akka actor fsm

我有以下演员

public class MyMainActor extends AbstractActor {

  MyClass myclass;
  ActorRef myFsm;


  public static Props props(MyClass myclass, ActorRef myfsm) {
    return Props.create(MyMainActor.class,
        () -> new MyMainActor(myclass, myfsm));
  }

  public MyMainActor(Myclass myclass, ActorRef myfsm) {
    this.myclass = myclass;
    this.myfsm = myfsm;
  }



  @Override
  public Receive createReceive() {
   // message handling here
  }


}

我正在测试演员为fllows

new TestKit(system) {
  {

    final ActorRef underTest =
        system.actorOf(MyMainActor.props(myclassMock, fsm));

    underTest.tell(new msgThatItcanHandle, getRef());
    expectMsgClass(msgafterhandling);
  }
};

这是两个论点

    static ActorRef fsm = system.actorOf(Props.create(MyFSM.class));
    myclassMock = Mockito.mock(MyClass.class);

这使我在初始化测试中的actor时出错

 java.lang.ExceptionInInitializerError
  at com.mathworks.batup.deployer.actors.MyTest$1.<init>(AppConfigReaderTest.java:62)
  at com.mathworks.batup.deployer.actors.MyTest.testApplicationConfig(MyTest.java:57)
  at sun.reflect.NativeMethodAccessorImpl.

  invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:498)
  at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
  at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
  at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
  at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
  at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
  at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
  at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
  at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
  at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
  at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
  at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
  at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
  at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
  at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
  at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
  at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
  at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.util.NoSuchElementException: key not found: 
  at scala.collection.MapLike.default(MapLike.scala:232)
  at scala.collection.MapLike.default$(MapLike.scala:231)
  at scala.collection.AbstractMap.default(Map.scala:59)
  at scala.collection.MapLike.apply(MapLike.scala:141)
  at scala.collection.MapLike.apply$(MapLike.scala:140)
  at scala.collection.AbstractMap.apply(Map.scala:59)
  at scala.concurrent.duration.Duration$.apply(Duration.scala:40)
  at scala.concurrent.duration.Duration$.create(Duration.scala:282)
  at scala.concurrent.duration.Duration.create(Duration.scala)
  at com.mathworks.batup.actors.MyMainActor.<clinit>(MyMainActor.java:43)
  ... 27 more

这里的不同之处在于,作为Props参数传入的ActorRef是一个有限状态机actor,它不扩展AbstractActor但扩展了AbstractFSM。这可能是问题吗?什么是通过引用有限状态机来初始化actor的正确方法

0 个答案:

没有答案