2017-01-20 13 views
2

私はこのようなテストを持っている:APIテスト+トランザクションのロールバック - 春ブーツ

@Test 
@Transactional 
public void when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered() { 
    //language=JSON 
    String startProductionOrder = "{\n" + 
      " \"productionOrder\": \"1700281\",\n" + 
      " \"staffCodeSignature\": \"00000425\"\n" + 
      "}"; 

    given() 
      .body(startProductionOrder) 
      .accept(ContentType.JSON) 
      .contentType(ContentType.JSON) 
      .when() 
      .post(commandPath) 
      .then().statusCode(201); 


    ProductionOrder po = productionOrderRepo.findOneByCode("1700281"); 
    assertThat(po.getCode(), is("1700281")); 
} 

私の意図はrestassured使用開始から終了までのREST APIをテストすることです。各コマンドを想像すると、コントローラから始めて@Transactionalという注釈が付けられます。問題は、テストが自動的にロールバックされないことです。デバッグログが示すよりも(投稿する長すぎたので、私は多くの部分を削除):

DEBUG 14120 --- [   main] tractDirtiesContextTestExecutionListener : Before test method: context [[email protected] testClass = ProductionOrderStartLT, testInstance = [email protected]9ed8, testMethod = when_EnteredRegisteredProductionOrder_and_[email protected]ProductionOrderStartLT, testExcep 
DEBUG 14120 --- [   main] t.a.AnnotationTransactionAttributeSource : Adding transactional method 'com.demo.logbook.commandValidationLT.ProductionOrderStartLT.when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
DEBUG 14120 --- [   main] t.c.t.TransactionalTestExecutionListener : Explicit transaction definition [PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''] found for test context [[email protected] testClass = ProductionOrderStartLT, testInstance = [email protected]9ed8, testMethod = when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_ 
DEBUG 14120 --- [   main] t.c.t.TransactionalTestExecutionListener : Retrieved @TransactionConfiguration [null] for test class [com.demo.logbook.commandValidationLT.ProductionOrderStartLT]. 
DEBUG 14120 --- [   main] t.c.t.TransactionalTestExecutionListener : Using TransactionConfigurationAttributes [[email protected] transactionManagerName = '', defaultRollback = true] for test class [com.demo.logbook.commandValidationLT.ProductionOrderStartLT]. 
DEBUG 14120 --- [   main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[[email protected] testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp 
DEBUG 14120 --- [   main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [[email protected] size = 1, maxSize = 32, parentContextCount = 0, hitCount = 1, missCount = 1] 
DEBUG 14120 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'transactionManager' 
DEBUG 14120 --- [   main] t.c.t.TransactionalTestExecutionListener : No method-level @Rollback override: using default rollback [true] for test context [[email protected] testClass = ProductionOrderStartLT, testInstance = [email protected]9ed8, testMethod = when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_Prod 
DEBUG 14120 --- [   main] o.s.orm.jpa.JpaTransactionManager  : Creating new transaction with name [com.demo.logbook.commandValidationLT.ProductionOrderStartLT.when_EnteredRegisteredProductionOrder_and_RegisteredStaffSignature_then_201_and_ProductionOrderRegistered]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
DEBUG 14120 --- [   main] o.s.orm.jpa.JpaTransactionManager  : Opened new EntityManager [[email protected]] for JPA transaction 
DEBUG 14120 --- [   main] o.s.orm.jpa.JpaTransactionManager  : Exposing JPA transaction as JDBC transaction [org.springframewo[email protected]465d1345] 
INFO 14120 --- [   main] o.s.t.c.transaction.TransactionContext : Began transaction (1) for test context [[email protected] testClass = ProductionOrderStartLT, testInstance = [email protected]9ed8, testMethod = when_EnteredRegisteredProductionOrder_and_[email protected]ProductionOrderStartLT 
DEBUG 14120 --- [   main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[[email protected] testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp 
DEBUG 14120 --- [   main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [[email protected] size = 1, maxSize = 32, parentContextCount = 0, hitCount = 2, missCount = 1] 
DEBUG 14120 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.boot.test.mock.mockito.MockitoBeans' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Initializing servlet 'dispatcherServlet' 
INFO 14120 --- [nio-9090-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]  : Initializing Spring FrameworkServlet 'dispatcherServlet' 
INFO 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : FrameworkServlet 'dispatcherServlet': initialization started 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'multipartResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Using MultipartResolver [org.springf[email protected]63880be9] 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Finished creating instance of bean 'org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Unable to locate LocaleResolver with name 'localeResolver': using default [[email protected]560b484d] 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Finished creating instance of bean 'org.springframework.web.servlet.theme.FixedThemeResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Unable to locate ThemeResolver with name 'themeResolver': using default [[email protected]c] 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'requestMappingHandlerMapping' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'viewControllerHandlerMapping' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'beanNameHandlerMapping' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'resourceHandlerMapping' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'defaultServletHandlerMapping' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'faviconHandlerMapping' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'welcomePageHandlerMapping' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'requestMappingHandlerAdapter' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'httpRequestHandlerAdapter' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'simpleControllerHandlerAdapter' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'errorAttributes' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'handlerExceptionResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Finished creating instance of bean 'org.springframework.web.servlet.view.DefaultRequestToViewNameTranslator' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Unable to locate RequestToViewNameTranslator with name 'viewNameTranslator': using default [org.spri[email protected]56aa150f] 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'beanNameViewResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'mvcViewResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'defaultViewResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'viewResolver' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Finished creating instance of bean 'org.springframework.web.servlet.support.SessionFlashMapManager' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Unable to locate FlashMapManager with name 'flashMapManager': using default [[email protected]c40b216] 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Published WebApplicationContext of servlet 'dispatcherServlet' as ServletContext attribute with name [org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcherServlet] 
INFO 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : FrameworkServlet 'dispatcherServlet': initialization completed in 19 ms 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Servlet 'dispatcherServlet' configured successfully 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.w.f.OrderedRequestContextFilter : Bound request context to thread: [email protected] 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : DispatcherServlet with name 'dispatcherServlet' processing POST request for [/commands/production/production-orders] 
DEBUG 14120 --- [nio-9090-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Looking up handler method for path /commands/production/production-orders 
DEBUG 14120 --- [nio-9090-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Returning handler method [public void com.demo.logbook.commands.web.controllers.ProductionController.startProductionOrder(com.demo.logbook.commands.commands.productionOrder.StartProductionOrder)] 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'productionController' 
DEBUG 14120 --- [nio-9090-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Opening JPA EntityManager in OpenEntityManagerInViewInterceptor 
DEBUG 14120 --- [nio-9090-exec-1] m.m.a.RequestResponseBodyMethodProcessor : Read [class com.demo.logbook.commands.commands.productionOrder.StartProductionOrder] as "application/json;charset=UTF-8" with [org.springframework.http.converter.json.MappingJackson2HttpMes[email protected]] 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.NotNullValidator' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'transactionManager' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Found thread-bound EntityManager [[email protected]] for JPA transaction 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Creating new transaction with name [com.demo.logbook.commands.web.controllers.ProductionController.startProductionOrder]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '',-java.lang.RuntimeException 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Exposing JPA transaction as JDBC transaction [org.springframewo[email protected]63fa0a64] 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.c.w.c.ProductionController   : Command received : StartProductionOrder{productionOrder='1700281'} SignedCommand{staffCodeSignature='00000425'} 
INFO 14120 --- [nio-9090-exec-1] o.h.h.i.QueryTranslatorFactoryInitiator : HHH000397: Using ASTQueryTranslatorFactory 
Hibernate: select eventtype0_.id as id1_17_, eventtype0_.name as name2_17_, eventtype0_.type as type3_17_ from events.event_type eventtype0_ where eventtype0_.name=? 
Hibernate: select aggregate0_.aggregate_id as aggregat1_15_, aggregate0_.name as name2_15_ from events.aggregate aggregate0_ where aggregate0_.name=? 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.event_store.services.EventStore : Flushing event : Event 
DEBUG 14120 --- [nio-9090-exec-1] stomAnnotationTransactionAttributeSource : Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'transactionManager' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Found thread-bound EntityManager [[email protected]] for JPA transaction 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Participating in existing transaction 
Hibernate: select nextval ('hibernate_sequence') 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.event_store.services.EventStore : Event flushed : Event{event_id=31, correlation_id=e5901e3d-9cbf-4c53-bb8c-1fb1006328f8, data='{"transaction_id":"0c4cb269-32fc-422d-93a8-28d506911861","staffCodeSignature":"00000425","productionOrderCode":"1700281"}', version=1, eventT[email protected]34ef5c1b, aggregate=Aggregate{a 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.c.w.c.ProductionController   : Publishing eventData : ProductionOrderStarted 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.c.w.c.ProductionController   : Command success : StartProductionOrder{productionOrder='1700281'} SignedCommand{staffCodeSignature='00000425'} 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'adminProjection' 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.a.projection.AdminProjection  : Event received in admin module: ProductionOrderStarted 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.a.s.AdminValidationService  : Validating event in admin module: ProductionOrderStarted 
Hibernate: select count(production0_.product_order_id) as col_0_0_ from admin.production_order production0_ where production0_.code=? 
Hibernate: select count(staff0_.reason_type_id) as col_0_0_ from admin.staff staff0_ where staff0_.code=? 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.a.s.AdminValidationService  : Event validation successful in admin module: ProductionOrderStarted 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.a.projection.AdminProjection  : Event processed successfully by admin module: ProductionOrderStarted 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'productionProjection' 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.p.ProductionProjection   : Event received in prod module: ProductionOrderStarted 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.s.ProductionValidationService : Validating event in prod module: ProductionOrderStarted 
Hibernate: select production0_.transaction_id as transact1_20_, production0_.last_action_timestamp as last_act2_20_, production0_.code as code3_20_, production0_.product_code as product_4_20_, production0_.product_name as product_5_20_, production0_.work_center_code as work_cen6_20_, production0_.work_center_name as work_cen7_20_ from prod.production_order production0_ where production0_.code=? 
2017-01-20 13:50:10.399 DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.s.ProductionValidationService : Event validation successful in prod module: ProductionOrderStarted 
Hibernate: select production0_.product_order_id as product_1_5_, production0_.code as code2_5_, production0_.product_id as product_3_5_, production0_.work_center_id as work_cen4_5_ from admin.production_order production0_ where production0_.code=? 
Hibernate: select product0_.product_id as product_1_3_0_, product0_.box_size as box_size2_3_0_, product0_.code as code3_3_0_, product0_.container_size as containe4_3_0_, product0_.container_type as containe5_3_0_, product0_.description as descript6_3_0_, product0_.markezini_capacity as markezin7_3_0_, product0_.partena_capacity as partena_8_3_0_, product0_.product_category_id as product_9_3_0_, productca 
Hibernate: select workcenter0_.work_center_id as work_cen1_13_0_, workcenter0_.code as code2_13_0_, workcenter0_.is_active as is_activ3_13_0_, workcenter0_.name as name4_13_0_, workcenter0_.wing_id as wing_id5_13_0_, wing1_.wing_id as wing_id1_12_1_, wing1_.name as name2_12_1_ from admin.work_center workcenter0_ left outer join admin.wing wing1_ on workcenter0_.wing_id=wing1_.wing_id where workcenter0_.w 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.s.ProductionEventsService  : Flushing production order: ProductionOrder(super=TransactionalEntity(transaction_id=0c4cb269-32fc-422d-93a8-28d506911861, lastActionTimestamp=2017-01-20T13:50:10.425), code=1700281, workCenterCode=0032, workCenterName=SYFPAC A, productCode=0100100, productName=AMINOPHYLLINE 250MG PPAMP 10ML, reasonCodesList=null, inProcessContro 
DEBUG 14120 --- [nio-9090-exec-1] stomAnnotationTransactionAttributeSource : Adding transactional method 'save' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; '' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'transactionManager' 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Found thread-bound EntityManager [[email protected]] for JPA transaction 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Participating in existing transaction 
Hibernate: select production0_.transaction_id as transact1_20_1_, production0_.last_action_timestamp as last_act2_20_1_, production0_.code as code3_20_1_, production0_.product_code as product_4_20_1_, production0_.product_name as product_5_20_1_, production0_.work_center_code as work_cen6_20_1_, production0_.work_center_name as work_cen7_20_1_, inprocessc1_.production_order_transaction_id as producti5_18 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.s.ProductionEventsService  : Flushed production order: ProductionOrder(super=TransactionalEntity(transaction_id=0c4cb269-32fc-422d-93a8-28d506911861, lastActionTimestamp=2017-01-20T13:50:10.425), code=1700281, workCenterCode=0032, workCenterName=SYFPAC A, productCode=0100100, productName=AMINOPHYLLINE 250MG PPAMP 10ML, reasonCodesList=null, inProcessControl 
DEBUG 14120 --- [nio-9090-exec-1] c.d.l.q.p.p.ProductionProjection   : Event processed successfully by prod module: ProductionOrderStarted 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Initiating transaction commit 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Committing JPA transaction on EntityManager [[email protected]] 
Hibernate: insert into events.event (aggregate_id, correlation_id, data, event_type_id, time_created, version, event_id) values (?, ?, ?, ?, ?, ?, ?) 
Hibernate: insert into prod.production_order (last_action_timestamp, code, product_code, product_name, work_center_code, work_center_name, transaction_id) values (?, ?, ?, ?, ?, ?, ?) 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.JpaTransactionManager  : Not closing pre-bound JPA EntityManager after transaction 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Null ModelAndView returned to DispatcherServlet with name 'dispatcherServlet': assuming HandlerAdapter completed request handling 
DEBUG 14120 --- [nio-9090-exec-1] o.j.s.OpenEntityManagerInViewInterceptor : Closing JPA EntityManager in OpenEntityManagerInViewInterceptor 
DEBUG 14120 --- [nio-9090-exec-1] o.s.orm.jpa.EntityManagerFactoryUtils : Closing JPA EntityManager 
DEBUG 14120 --- [nio-9090-exec-1] o.s.web.servlet.DispatcherServlet  : Successfully completed request 
DEBUG 14120 --- [nio-9090-exec-1] o.s.b.w.f.OrderedRequestContextFilter : Cleared thread-bound request context: [email protected] 
Hibernate: select production0_.transaction_id as transact1_20_, production0_.last_action_timestamp as last_act2_20_, production0_.code as code3_20_, production0_.product_code as product_4_20_, production0_.product_name as product_5_20_, production0_.work_center_code as work_cen6_20_, production0_.work_center_name as work_cen7_20_ from prod.production_order production0_ where production0_.code=? 
DEBUG 14120 --- [   main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[[email protected] testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp 
DEBUG 14120 --- [   main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [[email protected] size = 1, maxSize = 32, parentContextCount = 0, hitCount = 3, missCount = 1] 
DEBUG 14120 --- [   main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[[email protected] testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp 
DEBUG 14120 --- [   main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [[email protected] size = 1, maxSize = 32, parentContextCount = 0, hitCount = 4, missCount = 1] 
DEBUG 14120 --- [   main] o.s.b.f.s.DefaultListableBeanFactory  : Returning cached instance of singleton bean 'org.springframework.boot.test.mock.mockito.MockitoBeans' 
DEBUG 14120 --- [   main] c.DefaultCacheAwareContextLoaderDelegate : Retrieved ApplicationContext from cache with key [[[email protected] testClass = ProductionOrderStartLT, locations = '{}', classes = '{class com.demo.logbook.ElogbookApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.sp 
DEBUG 14120 --- [   main] org.springframework.test.context.cache : Spring test ApplicationContext cache statistics: [[email protected] size = 1, maxSize = 32, parentContextCount = 0, hitCount = 5, missCount = 1] 
DEBUG 14120 --- [   main] o.s.orm.jpa.JpaTransactionManager  : Initiating transaction rollback 
DEBUG 14120 --- [   main] o.s.orm.jpa.JpaTransactionManager  : Rolling back JPA transaction on EntityManager [[email protected]] 
DEBUG 14120 --- [   main] o.s.orm.jpa.JpaTransactionManager  : Closing JPA EntityManager [[email protected]] after transaction 
DEBUG 14120 --- [   main] o.s.orm.jpa.EntityManagerFactoryUtils : Closing JPA EntityManager 
INFO 14120 --- [   main] o.s.t.c.transaction.TransactionContext : Rolled back transaction for test context [[email protected] testClass = ProductionOrderStartLT, testInstance = [email protected]9ed8, testMethod = when_EnteredRegisteredProductionOrder_and_[email protected]ProductionOrderStart 
DEBUG 14120 --- [   main] tractDirtiesContextTestExecutionListener : After test method: context [[email protected] testClass = ProductionOrderStartLT, testInstance = [email protected]9ed8, testMethod = when_EnteredRegisteredProductionOrder_and_[email protected]ProductionOrderStartLT, testExcept 

新しいappplicationコンテキスト(およびトレッドが使用されている)ことを思われます。上記の作業をどのように達成できますか? @AfterTranscationを使用して各テストでデータベースをクリーンアップする唯一の方法はありますか?

+0

私はExecutionPhase.AFTER_TEST_METHODで@Sqlを使用して、各テスト後にテーブルを切り捨てます。しかし、私がそれらを並行して実行しようとすると、これは機能しません。だから私は、トランザクションのロールバックはまだ(と高速)便利ですと思う。 – Nikos

+0

解決方法を見つけましたか? – user2912611

+0

...私は締め切り後に再訪すると思います – Nikos

答えて

0

@Transactionalは、同じスレッド内でのみ動作します。したがって、Springによって提供されるロールバックは、トランザクションが開始されたスレッドで永続化されたオブジェクトに対してのみ機能します。

あなたの安心したテストでは、統合テストが実行され、別のスレッドでSpringのコンテキストにヒットします。したがって、あなたの安心したテストでは、テスト設定で保持されたオブジェクトを見ることができますが、Springはあなたの安心テストで永続化されたリソースを決してクリーンアップすることはできません。

これに対処する唯一の方法は、テストでエンティティを自分自身で削除することです(Junitルールを使用するか、テスト設定の状態をクリーンアップする)。

0

私はこの問題に対して私の頭を叩いていて、それに対する回避策を見つけることができました。この問題は、JUnitテストが埋め込みサーブレットコンテナとは異なるスレッドで実行されるという事実によって実際に発生しています。つまり、2つのトランザクションとデータベース接続が開かれ、互いに認識されません。修正は(フィルターを使用して)サーブレットコンテナにJUnitテストにオープンしたトランザクションを伝播することです:

@Transactional 
public class MyTest { 
    private static final String[] FIELD_NAMES = {"resources", "synchronizations", "currentTransactionName", "currentTransactionReadOnly", "currentTransactionIsolationLevel", "actualTransactionActive"}; 
    private static final Field[] FIELDS = new Field[FIELD_NAMES.length]; 
    private static final Object[] FIELD_VALUES = new Object[FIELD_NAMES.length]; 

    @Before 
    public void copyTransactionThreadLocals() { 
     for (int i=0; i<FIELD_NAMES.length; i++) { 
      Field field = ReflectionUtils.findField(TransactionSynchronizationManager.class,FIELD_NAMES[i]); 
      field.setAccessible(true); 
      FIELD_VALUES[i] = ((ThreadLocal)ReflectionUtils.getField(field,null)).get(); 
      FIELDS[i] = field; 
     } 
    } 

    @Configuration 
    static class Config { 
     @Bean 
     public Filter transactionFilter() { 
      return new OncePerRequestFilter() { 
       @Override 
       protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { 
        for (int i=0; i<FIELD_NAMES.length; i++) { 
         ((ThreadLocal)ReflectionUtils.getField(FIELDS[i],null)).set(FIELD_VALUES[i]); 
        } 
        filterChain.doFilter(request,response); 
       } 
      }; 
     } 
    } 
} 

上記のコードがアクセス不能の静的フィールドをコピーするためにリフレクションを使用するハックですのでご注意ください。