Skip to content

Injecting EXTENDED @PersistenceContext into JUnit 4 test class causes NoSuchBeanDefinitionException [SPR-8834] #13476

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue Nov 9, 2011 · 5 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: test Issues in the test module status: backported An issue that has been backported to maintenance branches type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Matthew T. Adams opened SPR-8834 and commented

I'm trying to use a container-managed extended EntityManager in a test class. When I use the annotation @PersistenceContext(type=PersistenceContextType.EXTENDED, unitName="..."), the PersistenceAnnotationBeanPostProcessor throws a NoSuchBeanDefinitionException saying that it can't find the bean with the name of my test class. If I remove the type argument (or set it to PersistenceContextType.TRANSACTION), my test fails for unrelated reasons.

Reproducible example attached.

To reproduce, extract archives in archive, issue "mvn install" in ../dn-atomikos/trunk, and then issue "mvn clean test -Dtest=TwoPhaseCommitTest" in ../db.jta/.

If reproduced, you should get an exception like the one below. If you comment out "type=PersistenceContextType.EXTENDED)" in AbstractTwoPhaseCommitTest, this exception goes away (and a new, unrelated exception may appear). I need to test this with a container-managed extended EntityManager, so I'm blocked until this is fixed.

2011-11-09 14:40:39,476 TRACE [org.springframework.beans.factory.support.DefaultListableBeanFactory] - <No bean named 'org.example.test.TwoPhaseCommitTest' found in org.springframework.beans.fact
ory.support.DefaultListableBeanFactory@4934ce4a: defining beans [transactionManager,org.springframework.context.support.ConversionServiceFactoryBean#0,targetDataSources,xaOrderDs,xaItemDs,xaTm,xa
Ut,ds,orderDs,itemDs,targetEmfs,orderEmf,itemEmf,jpaProps,abstractEmf,jpaImplProps,jpaImplJtaProps,jpaImplDbProps,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.trans
action.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,org
.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.int
ernalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,org.springframework.context.annotation.internalPersistenceAnnotationProcessor,org.springf
ramework.context.annotation.ConfigurationClassPostProcessor$ImportAwareBeanPostProcessor#0]; root of factory hierarchy>
2011-11-09 14:40:39,476 ERROR [org.springframework.test.context.TestContextManager] - <Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyIn
jectionTestExecutionListener@502cb49d] to prepare test instance [org.example.test.TwoPhaseCommitTest@766d4f30]>
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.example.test.TwoPhaseCommitTest': Injection of persistence dependencies failed; nested exception is org
.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.example.test.TwoPhaseCommitTest' is defined
        at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:342)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1099)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:377)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:110)
        at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:290)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
        at org.apache.maven.surefire.junit4.JUnit4TestSet.execute(JUnit4TestSet.java:53)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:123)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:104)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110)
        at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:175)
        at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:81)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:68)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.example.test.TwoPhaseCommitTest' is defined
        at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:529)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1095)
        at org.springframework.beans.factory.support.AbstractBeanFactory.isPrototype(AbstractBeanFactory.java:430)
        at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.resolveExtendedEntityManager(PersistenceAnnotationBeanPostProcessor.java:689)
        at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$PersistenceElement.getResourceToInject(PersistenceAnnotationBeanPostProcessor.java:629)
        at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150)
        at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
        at org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.postProcessPropertyValues(PersistenceAnnotationBeanPostProcessor.java:339)
        ... 32 more

Affects: 3.1 RC1

Attachments:

Referenced from: commits 592e344, 8fb7b69, 388561d, e48c315

Backported to: 4.0.7, 3.2.11

1 votes, 5 watchers

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Sam, could you take a look at this?

@spring-projects-issues
Copy link
Collaborator Author

Thomas Bétrancourt commented

I've the same issue. Any one knows a workarround ?

@spring-projects-issues
Copy link
Collaborator Author

Arnaud Camuset commented

Hello
I've the same issue to.
I did some test with Junit 4.10 but no success

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Seems like PersistenceAnnotationBeanPostProcessor has an assumption for a bean backed by a BeanDefinition there... I'll try to remove that, allowing for PABPP to consistently work for a non-managed target instance as well.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Fixed at the PersistenceAnnotationBeanPostProcessor level, and working in a unit test for autowiring an existing object there.

Juergen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) in: test Issues in the test module status: backported An issue that has been backported to maintenance branches type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants