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:
My Service is declared: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 application context is:Code:@Service public class UnsecureService implements IUnsecureService {
My testcase 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"/>
It refers to the following test context:Code:@ContextConfiguration(locations={"../application-test-context.xml"}) @RunWith(SpringJUnit4ClassRunner.class) public class TestPassword { @Autowired private UnsecureService unsecureService; @Test public void testResetPassword() { } }
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.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>
Spring 3.1.2
Java 1.6
Thanks,
Tom


Reply With Quote
