2017-01-20 4 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 사용하여 처음부터 끝까지 나머지 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]eb 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.springfr[email protected]ff21443] 
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 컨텍스트에 도달합니다. 따라서 여러분의 안심 테스트는 테스트 설정에서 유지 된 객체를 볼 수 있지만, 안심 테스트에서 지속 된 리소스를 자동으로 정리할 수는 없습니다.

유일한 방법은 테스트에서 엔티티를 삭제하는 것입니다 (Junit 규칙 사용 또는 테스트 설정에서 상태 정리).

0

저는이 문제에 대해 머리를 숙였으며 해결 방법을 찾아 냈습니다. 이 문제는 실제로 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); 
       } 
      }; 
     } 
    } 
} 

위의 코드에 액세스 할 수없는 정적 필드를 복사 반사를 사용하는 해킹 양해하여 주시기 바랍니다 : 수정 프로그램 (필터 사용) 서블릿 컨테이너에 JUnit 테스트에서 열린 트랜잭션을 전파하는 것입니다.