2015-04-24 53 views
0

我寫了一個基於WebSocket的Actor,它工作正常,但我需要測試。在接下來的方式使用JPA.withTransaction的測試Actor(java.lang.IllegalStateException:測試時關閉EntityManager)

我已經創建的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); 

    } 

} 

我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時它工作正常。

我應該如何更改我的測試,讓演員能夠訪問數據庫(有效訪問EntityManager)?

回答

0

更改的測試下一

`@Test 公共無效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); 
    }); 
} 

`

並轉換bulkImport方法爲靜態通過使有自()REF

bulkImport(format.importFromJson(content), out, this.self());