Announcement Announcement Module
Collapse
No announcement yet.
spring 2.0 test: how to override getAutowireMode? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring 2.0 test: how to override getAutowireMode?

    With Spring 2.0, it is no longer possible to override AbstractDependencyInjectionSpringContextTests.getA utowireMode().

    With Spring 1.x I was setting mode to AUTOWIRE_BY_NAME.

    With Spring 2.0 I have tried setting autowire="byName" on the <bean/> definitions, but I keep getting an error indicing that the classpath has more than one implementation of a type and I need to autowire by name.

    Code:
    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.bull.test.TestServiceTest': Unsatisfied dependency expressed through bean property 'companyService': There are 2 beans of type [interface com.bull.test.company.service.CompanyService] for autowire by type. There should have been exactly 1 to be able to autowire property 'companyService' of bean 'com.bull.test.TestServiceTest'. Consider using autowire by name instead.
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:803)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:709)
    	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:266)
    	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.injectDependencies(AbstractDependencyInjectionSpringContextTests.java:179)
    	at org.springframework.test.AbstractDependencyInjectionSpringContextTests.prepareTestInstance(AbstractDependencyInjectionSpringContextTests.java:158)
    	at com.bull.test.BsoaIdeClientTests.prepareTestInstance(BsoaIdeClientTests.java:92)
    	at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:76)
    	at junit.framework.TestCase.runBare(TestCase.java:125)
    	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
    	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
    Now in spring 2.0, getAutowireMode is final. The javadocs indicate it is now set by injection, but I cannot find anything that indicates I need to use something other than the autowire attribute of <bean/>.

    Can someone point me to docs describing how to configure autowire mode for JUnit tests?


    Code:
    	/**
    	 * Set the autowire mode for test properties set by Dependency Injection.
    	 * <p>The default is "AUTOWIRE_BY_TYPE". Can be set to "AUTOWIRE_BY_NAME"
    	 * or "AUTOWIRE_NO" instead.
    	 * @see #AUTOWIRE_BY_TYPE
    	 * @see #AUTOWIRE_BY_NAME
    	 * @see #AUTOWIRE_NO
    	 */
    	public final void setAutowireMode(int autowireMode) {
    		this.autowireMode = autowireMode;
    	}
    
    	/**
    	 * Return the autowire mode for test properties set by Dependency Injection.
    	 */
    	public final int getAutowireMode() {
    		return autowireMode;
    	}
    Thanks, Michael Giroux

  • #2
    You can set the autowire mode when the test is constructed -
    public MyTest()
    {
    setAutowireMode(AUTOWIRE_BY_NAME);
    }

    Comment


    • #3
      Thank You

      Thanks for the help. I'm on to Spring 2.0.

      Michael

      Comment


      • #4
        This is fine if you extend AbstractDependencyInjectionSpringContextTests that works for JUnit 3.8.

        What is the equivalent if you use a JUnit 4.4 @RunWith(SpringJUnit4ClassRunner.class) ?

        Comment


        • #5
          Did you ever figure this out? I have the same issue...

          Comment

          Working...
          X