17
One of my integration tests uses multiple Spring context files. It seems that Spring only autowires in beans from the first context and not the second. Does anyone know what I am doing wrong or how to work around the problem?
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:/META-INF/spring/applicationContext.xml",
"classpath:/META-INF/spring/applicationContext-security.xml"})
@Configurable
public class UserDetailsServiceImplIntegrationTest {
@Autowired
UserDataOnDemand dod;
// @Autowired does not work for this bean from applicationContext-security.xml
UserDetailsService userDetailsService;
@Before
public void setup() {
dod.init();
// workaround for autowiring problem
userDetailsService = (UserDetailsService)ctx.getBean("userDetailsService");
}
@Test
public void testLoadUser() {
UserDetails ud = userDetailsService.loadUserByUsername("david@somewhere.co.za");
Assert.assertEquals("david@somewhere.co.za", ud.getUsername());
}
}
I am using Spring 3.0.3.
Here is the stack trace when I uncomment the @Autowired line for UserDetailsService:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'za.co.acme.app.security.UserDetailsServiceImplIntegrationTest': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.springframework.security.core.userdetails.UserDetailsService za.co.acme.app.security.UserDetailsServiceImplIntegrationTest.userDetailsService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.security.core.userdetails.UserDetailsService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframe