0

I have been breaking my head in getting Spring @Autowired to work inside maven tests. When I run the JUnit tests inside IntellJ (did not try Eclipse) it works. But when I run mvn clean install, the JUnit tests fail with the following error

testApp(com.sample.spring.AppTest): Error creating bean with name 'com.sample.spring.AppTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.sample.spring.AppB com.sample.spring.AppTest.appB; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.sample.spring.AppB] 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)}

I have created a self contained sample project that exhibits this behaviour consistently. I am using Spring 3.1.1. I am sure someone faced the same issue and cracked it. Looking for some pointers regarding this issue.

skaffman
  • 398,947
  • 96
  • 818
  • 769

1 Answers1

1

This is a build path issue:

Working in eclipse I changed your ContextConfiguration to :

@ContextConfiguration(locations = "classpath:applicationContext.xml")

and it runs both with standard eclipse runner and maven(maven test or maven install).

Make sure you have:

src/main/java

src/test/java and

src/test/resources

declared as source folders in your buildpath

Community
  • 1
  • 1
Michael W
  • 3,515
  • 8
  • 39
  • 62
  • Thanks for your help. I was breaking my head as well and realized that my src/test/resources was the issue. I had it at src/test/java/resources and you pointed that as well. – user1361876 Apr 30 '12 at 15:45