使用JPA.withTransaction的Test Actor(java.lang.IllegalStateException:EntityManager在测试时关闭)

时间:2015-04-24 10:47:48

标签: java jpa playframework websocket akka

我写了一个基于WebSocket的Actor正常工作,但我需要测试。

我已经以下一种方式创建了WebSocket:

public static WebSocket<JsonNode> bulkEventsImportWebSocket() {
   return WebSocket.withActor(EventsImportActor::props);
}

我的演员是下一个:

public class EventsImportActor extends UntypedActor {

   public static final Injector INJECTOR = Guice.createInjector(new DefaultModule());

   private static ObjectMapper mapper = INJECTOR.getInstance(ObjectMapper.class);

   public static Props props(ActorRef out) {
      Logger.info("EventsImportActor created");
      return Props.create(EventsImportActor.class, out);
   }

   private final ActorRef out;

   public EventsImportActor(ActorRef out) {
      this.out = out;
   }

   @Override
   public void onReceive(Object message) throws Exception {
      Logger.info("EventsImportActor received message");
/*
      self().tell(PoisonPill.getInstance(), self());
*/
      try {

         JsonNode content = (JsonNode) message;

         JPA.withTransaction(() -> {
            someHugeImportOperationWithJPA(content)
         });
         Logger.info("EventsImportActor  message processed");
      } catch (Throwable e) {
         out.tell(ControllerUtils.getErrosObjectNode(e, mapper), self());
      }
   }
}

我写了下一个测试:

public class EventsImportActorTest extends WithServer {


   public static final Injector INJECTOR = Guice.createInjector(new DefaultModule());
   private static ObjectMapper mapper = INJECTOR.getInstance(ObjectMapper.class);

   static ActorSystem system;


   @BeforeClass
   public static void setUp(){
      system = ActorSystem.create();
      Logger.info("setUp done");
   }

   @AfterClass
   public static void stopApp() {
      system.shutdown();
   }


 @Test
   public void bulkEventsImportWebSocketTest() throws Throwable {


      Props propsTest = Props.create(EventsImportTestActor.class);
      final ActorRef testActorRef = system.actorOf(propsTest);

      Props propsImport = EventsImportActor.props(testActorRef);
      ActorRef importActorRef = system.actorOf(propsImport);

      importActorRef.tell(mapper.readTree(""), testActorRef);

   }

}

My EventsImportTestActor将进行断言,现在看起来像这样:

public class EventsImportTestActor extends UntypedActor {
   @Override
   public void onReceive(Object message) throws Exception {
      Logger.info("received by EventsImportTestActor message: " + message);
   }
}

当我运行此测试时,我收到java.lang.IllegalStateExceptionEntityManager在测试时关闭 当我不使用JPA时,它正常工作。

我应该如何更改我的测试以向演员提供对DB的访问权限(对EntityManager的有效访问权限)?

1 个答案:

答案 0 :(得分:0)

将测试更改为下一个

`@Test     public void bulkEventsImportWebSocketTest()抛出Throwable {

    Props propsTest = Props.create(EventsImportTestActor.class);
    final ActorRef testActorRef = system.actorOf(propsTest);

    EventJsonDataFormat format = new EventJsonDataFormat(mapper, occurrenceDAO);
    JsonNode content = mapper.readTree("");

    Logger.info("content: " + content);
    JPA.withTransaction(() -> {
        EventsImportActor.bulkImport(format.importFromJson(content), testActorRef, testActorRef);
    });
}

`

并通过传递self()ref

将bulkImport方法转换为static
bulkImport(format.importFromJson(content), out, this.self());