Playframework 2.0 当存在多个测试用例时,播放2.0 Akka系统关闭

Playframework 2.0 当存在多个测试用例时,播放2.0 Akka系统关闭,playframework-2.0,akka,Playframework 2.0,Akka,我有两个控制器,都使用2.0版中的AKKA演员。因此,有两个测试用例针对这两个API进行测试。但是,当执行“播放测试”时,只有一个测试用例会成功,另一个会失败。如果我单独运行它们,它将成功运行。我的预感是演员系统已经被第一次测试关闭了。然而,我对2号和Akka是新手,这只是我的猜测。有解决办法吗 @Test public void callPostA() { running(testServer(2222, fakeApplication(inMemoryDatabase())), new Ru

我有两个控制器,都使用2.0版中的AKKA演员。因此,有两个测试用例针对这两个API进行测试。但是,当执行“播放测试”时,只有一个测试用例会成功,另一个会失败。如果我单独运行它们,它将成功运行。我的预感是演员系统已经被第一次测试关闭了。然而,我对2号和Akka是新手,这只是我的猜测。有解决办法吗

@Test
public void callPostA() {
running(testServer(2222, fakeApplication(inMemoryDatabase())), new Runnable() {
        @Override
        public void run() {
            HttpPost httpPost = new HttpPost("http://localhost:2222/controllera");
            ....
        }
    });
}
@Test
public void callPostB() {
running(testServer(2222, fakeApplication(inMemoryDatabase())), new Runnable() {
        @Override
        public void run() {
            HttpPost httpPost = new HttpPost("http://localhost:2222/controllerb");
            ....
        }
    });
}
两个控制器如下所示:

public class PostA extends Controller {
    // master actor for workers
    public static ActorRef masterActorA = Akka.system().actorOf(new Props(new UntypedActorFactory() {
    public UntypedActor create() {
          return new PostAActorMaster(Config.NUMBER_OF_WORKER_ACTOR);
       }
        }), "PostAActorMaster");

    public static Result postA() {

        Map<String, String[]> dict = body.asFormUrlEncoded();
        String paramField1 = dict.get("paramField1");
        String paramField2 = dict.get("paramField2");

        ProductInfo pInfo = new ProductInfo(paramField1, paramField2);
        ProductMessage pMessage = new ProductMessage(pInfo);
        return async(
        Akka.asPromise(ask(masterActorA, pMessage, 15000)).map(
            new Function<Object, Result>() {
                        ...
                        }
                ));
}

public class PostB extends Controller {
    // master actor for workers
    public static ActorRef masterActorB = Akka.system().actorOf(new Props(new UntypedActorFactory() {
    public UntypedActor create() {
          return new PostBActorMaster(Config.NUMBER_OF_WORKER_ACTOR);
       }
        }), "PostBActorMaster");

    public static Result postB() {

        Map<String, String[]> dict = body.asFormUrlEncoded();
        String paramField3 = dict.get("paramField3");
        String paramField4 = dict.get("paramField4");

        BillInfo bInfo = new BillInfo(paramField3, paramField4);
        BillMessage pMessage = new BillMessage(bInfo);
        return async(
        Akka.asPromise(ask(masterActorB, pMessage, 15000)).map(
            new Function<Object, Result>() {
                        ...
                        }
                ));
}
托管对象:

public class ProductInfo extends Model {
        @Id
        private String productId;
        ...
   }

我再也看不出这个问题了。下面是我的测试用例的结构。也许你可以试试看它是否适合你

斯卡拉:

object LoginApiTest extends PlaySpecification {
  "login api quick login" should {
    "post login data" in new WithCleanTestData {
      var org1 = new OrgInfo("testorg", "Test Inc");
      org1.save();
    }
  }
}


abstract class WithCleanTestData extends WithApplication(FakeApplication(
  additionalConfiguration = TestConf.getConf.toMap
  )) {
  override def around[T: AsResult](t: => T): Result = super.around {
    prepareDbWithData()
    t
  }
  def prepareDbWithData() = {
      OrgInfo.getAllOrgInfo.foreach(_.delete)
  }
}
爪哇:


嘿,我使用的是静态ActorSystem,这导致了一个问题

[error] sbt.ForkMain$ForkError: Error creating bean with name 'accountServiceController' defined in file [/Users/admin/Development/src/totes/app/target/scala-2.11/classes/controllers/Ac‌​countServiceController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [controllers.AccountServiceController]: Constructor threw exception; nested exception is java.lang.IllegalStateException: cannot create children while terminating or terminated
通过在每次应用程序启动时创建一个动态ActorSystem,问题似乎消失了。
我在这方面做了一段时间,所以我想把它作为一个潜在的解决方案发布在这里。

你找到解决方案了吗?我在2.2.4中也遇到了同样的问题-2.3 akka spring使用play2 actor系统。[错误]sbt.ForkMain$ForkError:创建名为“accountServiceController”的bean时出错,该名称在文件[/Users/admin/Development/src/totes/app/target/scala-2.11/classes/controllers/accountServiceController.class]中定义:bean实例化失败;嵌套异常为org.springframework.beans.BeanInstantiationException:无法实例化bean类[controllers.AccountServiceController]:构造函数引发异常;嵌套异常是java.lang.IllegalStateException:无法在终止或终止时创建子项使用静态actorsystem导致我出现此问题。
public class MyHelpers extends Helpers {
    public static FakeApplication myFakeApplication(Map<String,String> additionalConfiguration) {
        List<String> withoutPlugins = new ArrayList<String>();
        List<String> additionalPlugins = new ArrayList<String>();
        return new MyFakeApplication(new java.io.File("."), MyHelpers.class.getClassLoader(),
            additionalConfiguration, withoutPlugins, additionalPlugins, null);
    }
}

public class BaseModelTest extends WithApplication {
    @Before
    public void before() {
    }
}

public class PostTest extends BaseModelTest {

    @Test
    public void test1() {
    }
}
parallelExecution in Test := false
[error] sbt.ForkMain$ForkError: Error creating bean with name 'accountServiceController' defined in file [/Users/admin/Development/src/totes/app/target/scala-2.11/classes/controllers/Ac‌​countServiceController.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [controllers.AccountServiceController]: Constructor threw exception; nested exception is java.lang.IllegalStateException: cannot create children while terminating or terminated