1

i have a junit test class which its configuration is as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({ WebContextTestExecutionListener.class,
        DependencyInjectionTestExecutionListener.class,
        DirtiesContextTestExecutionListener.class,
        TransactionalTestExecutionListener.class })
@ActiveProfiles("test")
@DirtiesContext
@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = {
        SpringConfig.class, SpringTestingConfig.class,
        SpringLocalContainerJPAConfig.class, CustomConfiguration.class })

and the test methods is calling service methods and the service class has the annotation @Transactional for some reason, when i make test case for service method it fails and give me the exception org.hibernate.PersistentObjectException: detached entity passed to persist as in this post: org.hibernate.PersistentObjectException: detached entity passed to persist with H2 in memory database and to fix this exception i have to annotate the test class with the annotation @Transactional although in the managed bean when i invoke same code it works fine, so my question is why @Transactional is required sometimes in test and when i should annotate my test class with @Transactional considering that service layer has this annotation.

Community
  • 1
  • 1
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • Could you post your test method and your managed bean code snippet? – Yugang Zhou Jul 28 '13 at 01:10
  • @Hippoom here's the link for full example: http://stackoverflow.com/questions/17887055/org-hibernate-persistentobjectexception-detached-entity-passed-to-persist-with/17894200#comment26146894_17894200 – Mahmoud Saleh Jul 28 '13 at 07:41

0 Answers0