来自默认调度程序的未捕获错误导致 JavaTestKit 出现致命错误

Uncaught error from default dispatcher causes fatal error with JavaTestKit

我刚刚接触 Akka。我正在尝试使用来自此 Maven 依赖项的 JavaTestKit 编写 JUNit 测试:

    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-actor_2.10</artifactId>
        <version>2.3.12</version>
    </dependency>
    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-testkit_2.11</artifactId>
        <version>2.3.12</version>
    </dependency>

即使是什么都不做的 JavaTestKit 实例也会抛出致命异常,因此我怀疑存在配置问题。 class 将重现问题:

import akka.actor.ActorSystem;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.testkit.JavaTestKit;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

public class GameTest {
  static ActorSystem system;
  static LoggingAdapter log;

  @BeforeClass
  public static void setup() {
    system = ActorSystem.create();
    log = Logging.getLogger(system, GameTest.class);
  }

  @AfterClass
  public static void teardown() {
      JavaTestKit.shutdownActorSystem(system);
      system = null;
  }

  @Test
  public void gutterGameTest() {
      log.info("gutterGameTest started");
      new JavaTestKit(system) {{
        // even when empty, the exception is thrown during initialization
      }};
  }
}

例外情况:

[ERROR] [08/20/2015 07:55:10.614] [default-akka.actor.default-dispatcher-4] [ActorSystem(default)] Uncaught error from thread [default-akka.actor.default-dispatcher-4] shutting down JVM since 'akka.jvm-exit-on-fatal-error' is enabled
java.lang.NoSuchMethodError: scala.Predef$.ArrowAssoc(Ljava/lang/Object;)Ljava/lang/Object;
    at akka.testkit.CallingThreadDispatcherQueues.registerQueue(CallingThreadDispatcher.scala:64)
    at akka.testkit.CallingThreadMailbox$$anon.initialValue(CallingThreadDispatcher.scala:318)
    at akka.testkit.CallingThreadMailbox$$anon.initialValue(CallingThreadDispatcher.scala:315)
    at java.lang.ThreadLocal.setInitialValue(ThreadLocal.java:180)
    at java.lang.ThreadLocal.get(ThreadLocal.java:170)
    ...

我使用的是默认配置。我还需要设置什么才能使 JavaTestKit 正常工作吗?

我想通了。它与 Akka 和 Testkit 库的不兼容组合有关(Scala 2.10 与 2.11)。以下 Maven 依赖项按预期工作:

    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-actor_2.10</artifactId>
        <version>2.3.12</version>
    </dependency>
    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-testkit_2.10</artifactId>
        <version>2.3.12</version>
    </dependency>
    <dependency>
        <groupId>org.scala-lang</groupId>
        <artifactId>scala-library</artifactId>
        <version>2.10.5</version>
    </dependency>