Announcement Announcement Module
Collapse
No announcement yet.
Strangeness while running tests and using Provider. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Strangeness while running tests and using Provider.

    Hi all,

    i am running into the strange error, when running jUnit tests with Provider<>. Only first tests succeeds, after second one i get an exception about not being able to autowire fields.

    Base test class:

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "/spring/hibernate.xml", "/spring/testDataSource.xml" })
    @TransactionConfiguration(transactionManager = "txManager", defaultRollback = true)
    public abstract class BaseModelTest {
    
    }
    Test class:

    Code:
    @Component
    public class AttachmentTest extends BaseModelTest {
    
        @Inject
        private Provider<AttachmentBuilder> attachmentBuilderProvider;
    
        @Test
        @Transactional
        public void testOne() {
            attachmentBuilderProvider.get();
        }
    
        @Test
        @Transactional
        public void testTwo() {
            attachmentBuilderProvider.get();
        }
    }
    AttachmentBuilder class:

    Code:
    @Transactional
    @Component
    @Scope("prototype")
    public class AttachmentBuilder {
    
    }

    When i run junit tests, it always fails on second test (testTwo()) if i run them separately - everything is ok. Any ideas on this?

    Code:
    <....>
    Jun 27, 2012 11:22:26 AM org.springframework.test.context.transaction.TransactionalTestExecutionListener startNewTransaction
    INFO: Began transaction (1): transaction manager [[email protected]e758ca]; rollback [true]
    Jun 27, 2012 11:22:26 AM org.springframework.test.context.transaction.TransactionalTestExecutionListener endTransaction
    INFO: Rolled back transaction after test execution for test context [[TestContext@68f51766 testClass = AttachmentTest, testInstance = my.namespace.model.test.AttachmentTest@32d35f5f, testMethod = testOne@AttachmentTest, testException = [null], mergedContextConfiguration = [MergedContextConfiguration@531a700a testClass = AttachmentTest, locations = '{classpath:/spring/hibernate.xml, classpath:/spring/testDataSource.xml}', classes = '{}', activeProfiles = '{}', contextLoader = 'org.springframework.test.context.support.DelegatingSmartContextLoader']]]
    Jun 27, 2012 11:22:26 AM org.springframework.test.context.TestContextManager prepareTestInstance
    SEVERE: Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@2f87fafc] to prepare test instance [my.namespace.model.test.AttachmentTest@86ecec5]
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'my.namespace.model.test.AttachmentTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.inject.Provider my.namespace.model.test.AttachmentTest.attachmentBuilderProvider; nested exception is java.lang.IllegalArgumentException: Can not set javax.inject.Provider field my.namespace.model.test.AttachmentTest.attachmentBuilderProvider to my.namespace.model.builder.AttachmentBuilder
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:374)
    	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:47)
    	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    	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:300)
    	at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.inject.Provider my.namespace.model.test.AttachmentTest.attachmentBuilderProvider; nested exception is java.lang.IllegalArgumentException: Can not set javax.inject.Provider field my.namespace.model.test.AttachmentTest.attachmentBuilderProvider to my.namespace.model.builder.AttachmentBuilder
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:506)
    	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
    	... 26 more
    Caused by: java.lang.IllegalArgumentException: Can not set javax.inject.Provider field my.namespace.model.test.AttachmentTest.attachmentBuilderProvider to my.namespace.model.builder.AttachmentBuilder
    	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
    	at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
    	at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
    	at java.lang.reflect.Field.set(Field.java:675)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502)
    	... 28 more

  • #2
    Why is your testcase an @Component, that shouldn't be?!

    You might want to crank up the logging to debug to get some more information.

    Comment


    • #3
      yeh just came across the same thing upgrading from 3.1.0 to 3.1.1
      apparently it's a bug fixed in 3.1.3
      https://jira.springsource.org/browse/SPR-9630

      Comment

      Working...
      X