Announcement Announcement Module
Collapse
No announcement yet.
Annotations not working in Unit test Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Annotations not working in Unit test

    I have created a unit test for my project. It is Spring 3.1.2, Tomcat. Spring Data, JPA and Eclipselink. Everything works if I deploy to Tomcat, but when I try and run tests, I get errors that it cannot find the injected beans.

    I get the following exception:

    Code:
    Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.companyName.ppsUIServer.unsecure.UnsecureService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:952)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:821)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
    	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
    	... 28 more
    My Service is declared:

    Code:
    @Service
    public class UnsecureService implements IUnsecureService {
    My application context is:

    Code:
    	<context:property-placeholder location="classpath*:META-INF/spring/profile.properties"/>
    	
    	<!-- Annotation scan -->
    	<context:annotation-config/>
        <context:component-scan base-package="com.companyName"/>
        
        <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>
    My testcase is:

    Code:
    @ContextConfiguration(locations={"../application-test-context.xml"})
    @RunWith(SpringJUnit4ClassRunner.class)
    public class TestPassword {
    
    	@Autowired
    	private UnsecureService unsecureService;
    
    	@Test
    	public void testResetPassword() {
    	}
    
    }
    It refers to the following test context:

    Code:
    <beans
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:task="http://www.springframework.org/schema/task"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
            http://www.springframework.org/schema/task 
    		http://www.springframework.org/schema/task/spring-task-3.1.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
    
    	 	<import resource="classpath:/META-INF/spring/application-root-context.xml"/> 
    	 	<import resource="classpath:/META-INF/spring/application-persistence-config.xml"/>
    	 	<import resource="classpath:/META-INF/spring/application-jpa-config.xml"/>
    
    </beans>
    I've not had this issue before and I've been using spring for a long time. So, I must be missing something stupid, but for the life of me, I can't figure out what.

    Spring 3.1.2
    Java 1.6


    Thanks,
    Tom

  • #2
    Hi Tom,

    It's likely just a small configuration issue since Spring cannot find your UnsecureService bean in the application context loaded for your test.

    You list excerpts from two application contexts, but you don't provide the names for either of those.

    So are you sure that "../application-test-context.xml", which you reference from your integration test, actually includes the component scanning that picks up your UnsecureService (i.e., either directly within the "../application-test-context.xml" file itself or within an XML configuration file that is imported into "../application-test-context.xml")?

    Regards,

    Sam

    Comment

    Working...
    X