Results 1 to 2 of 2

Thread: Autowiring of beans generated by EasyMock factory-method?

Threaded View

  1. #1
    Join Date
    Jun 2011
    Posts
    8

    Default Autowiring of beans generated by EasyMock factory-method?

    I have a problem that seems really strange to me.

    I have the following setup

    An interface:
    Code:
    package com.example;
    public interface SomeDependency {
    }
    A class:
    Code:
    package com.example;
    @Component
    public class SomeClass {
    }
    A spring config:
    Code:
    <beans ....>
        <context:component-scan base-package="com.example"/>
    
        <bean id="someInterfaceMock" class="org.easymock.EasyMock" factory-method="createMock">
            <constructor-arg value="com.example.SomeDependency" />
        </bean> 
    </beans>
    And a unit test:
    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration("/testconfig.xml")
    public class SomeClassTest {
    
        @Autowired
        SomeClass someClass;
    
        @Autowired
        SomeDependency someDependency;
    
        @Test
        public void testSomeClass() throws Exception {
            assertNotNull(someClass);
        }
    
        @Test
        public void testSomeDependency() throws Exception {
            assertNotNull(someDependency);
        }
    }
    The project compiles and the tests pass without any problem, i.e. autowiring of both SomeClass (a "real" object) and SomeDependency (a mock object generated by EasyMock) succeed.

    However, if I change the implementation of SomeClass to:

    Code:
    @Component
    public class SomeClass {
    
        @Autowired
        SomeDependency someDependency;
    }
    both tests fail because

    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [com.example.SomeDependency] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Aut owired(required=true)}


    So my questions are:
    1. Why does Spring fail to autowire the dependency in the SomeClass (when it succeeds autowiring the same dependency in the SomeClassTest)?
    2. What is the appropriate solution?



    /Mattias
    Last edited by matsev; Jun 13th, 2011 at 09:25 AM. Reason: Replaced bean declaration of SomeClass with component-scan.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •