-
Dec 28th, 2012, 04:46 PM
#1
Unit test of writer with job parameters in step scope
Hi,
I have trouble getting a JUnit test working which tests an ItemWriterAdapter by use of a self-implemented LdapTestUser helper class. I have already learned that TestScopeExecutionListener has to be used to make the params available and that the two above mentioned classes have to be declared as interfaces in the test class. LdapTestUser references an LdapTemplate bean which I do not get wired neither by defining an xml property nor an Autowired annotation in LdapTestUser.
Without job parameters and step scope attributes it works fine.
Here is the stack trace for annotation:
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'scopedTarget.ldapTestUser': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method: public void com.siemens.ces.sdsync.LdapTestUserImpl.setLdapTem plate(org.springframework.ldap.core.LdapTemplate); nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [org.springframework.ldap.core.LdapTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor.postProcessPropert yValues(AutowiredAnnotationBeanPostProcessor.java: 287)
...
Caused by: org.springframework.beans.factory.BeanCreationExce ption: Could not autowire method: public void com.siemens.ces.sdsync.LdapTestUserImpl.setLdapTem plate(org.springframework.ldap.core.LdapTemplate); nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [org.springframework.ldap.core.LdapTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor$AutowiredMethodEle ment.inject(AutowiredAnnotationBeanPostProcessor.j ava:593)
... 39 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No matching bean of type [org.springframework.ldap.core.LdapTemplate] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultL istableBeanFactory.raiseNoSuchBeanDefinitionExcept ion(DefaultListableBeanFactory.java:924)
... 41 more
and here for xml property
org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'scopedTarget.ldapTestUser' defined in class path resource [test-launch-context.xml]: Initialization of bean failed; nested exception is org.springframework.beans.ConversionNotSupportedEx ception: Failed to convert property value of type '$Proxy15 implementing org.springframework.ldap.core.LdapOperations,org.s pringframework.beans.factory.InitializingBean,org. springframework.aop.scope.ScopedObject,java.io.Ser ializable,org.springframework.aop.framework.AopInf rastructureBean,org.springframework.aop.SpringProx y,org.springframework.aop.framework.Advised' to required type 'org.springframework.ldap.core.LdapTemplate' for property 'ldapTemplate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing org.springframework.ldap.core.LdapOperations,org.s pringframework.beans.factory.InitializingBean,org. springframework.aop.scope.ScopedObject,java.io.Ser ializable,org.springframework.aop.framework.AopInf rastructureBean,org.springframework.aop.SpringProx y,org.springframework.aop.framework.Advised] to required type [org.springframework.ldap.core.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:527)
...
Caused by: org.springframework.beans.ConversionNotSupportedEx ception: Failed to convert property value of type '$Proxy15 implementing org.springframework.ldap.core.LdapOperations,org.s pringframework.beans.factory.InitializingBean,org. springframework.aop.scope.ScopedObject,java.io.Ser ializable,org.springframework.aop.framework.AopInf rastructureBean,org.springframework.aop.SpringProx y,org.springframework.aop.framework.Advised' to required type 'org.springframework.ldap.core.LdapTemplate' for property 'ldapTemplate'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing org.springframework.ldap.core.LdapOperations,org.s pringframework.beans.factory.InitializingBean,org. springframework.aop.scope.ScopedObject,java.io.Ser ializable,org.springframework.aop.framework.AopInf rastructureBean,org.springframework.aop.SpringProx y,org.springframework.aop.framework.Advised] to required type [org.springframework.ldap.core.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
at org.springframework.beans.BeanWrapperImpl.convertI fNecessary(BeanWrapperImpl.java:485)
... 37 more
Caused by: java.lang.IllegalStateException: Cannot convert value of type [$Proxy15 implementing org.springframework.ldap.core.LdapOperations,org.s pringframework.beans.factory.InitializingBean,org. springframework.aop.scope.ScopedObject,java.io.Ser ializable,org.springframework.aop.framework.AopInf rastructureBean,org.springframework.aop.SpringProx y,org.springframework.aop.framework.Advised] to required type [org.springframework.ldap.core.LdapTemplate] for property 'ldapTemplate': no matching editors or conversion strategy found
at org.springframework.beans.TypeConverterDelegate.co nvertIfNecessary(TypeConverterDelegate.java:241)
at org.springframework.beans.BeanWrapperImpl.convertI fNecessary(BeanWrapperImpl.java:470)
... 43 more
Here is the xml config file:
test-launch-context.xml
<bean id="ldapTestUser" class="com.siemens.ces.sdsync.LdapTestUserImpl" scope="step">
<property name="ldapTemplate" ref="ldapTemplate"/>
</bean>
<bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate" scope="step">
<property name="contextSource" ref="contextSource" />
</bean>
<bean id="contextSource" class="org.springframework.ldap.core.support.LdapC ontextSource" scope="step">
<property name="url" value="#{jobParameters['sd.url']}" />
<property name="userDn" value="#{jobParameters['sd.account']}" />
<property name="password" value="#{jobParameters['sd.pwd']}" />
</bean>
Here is the test class:
@ContextConfiguration(locations={"/test-launch-context.xml", "/launch-context.xml"})
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
public class ItemWriterAdapterTests {
@Autowired
private ItemWriter<User> itemWriterAdapter;
@Autowired
private LdapTestUser ldapTestUser;
public StepExecution getStepExecution() {
return MetaDataInstanceFactory.createStepExecution(new JobParametersBuilder().addString("sd.url", "ldap://localhost/ou=users,ou=getaccess,o=comp")
.addString("account", "abc")
.addString("pwd", "xyz")
.toJobParameters());
}
@Test
public void testWriteTwice() throws Exception {
ArrayList<User> users = new ArrayList<User>();
User user = new User("abc");
users.add(user1);
itemWriterAdapter.write(users);
User fromLdap = ldapTestUser.getUser("abc");
assertEquals(fromLdap, user);
and here the LdapTestUser class and interface:
public class LdapTestUserImpl implements LdapTestUser {
@Autowired
private LdapTemplate ldapTemplate;
public void setLdapTemplate(LdapTemplate ldapTemplate) {
this.ldapTemplate = ldapTemplate;
}
private static class UserContextMapper implements ContextMapper {
public Object mapFromContext(Object ctx) {
DirContextAdapter context = (DirContextAdapter) ctx;
User user = new User(context.getStringAttribute("abc"));
return user;
}
}
public User getUser(String gid) {
return (User) ldapTemplate.searchForObject("", "(cn=" + gid + ")", new UserContextMapper());
}
}
public interface LdapTestUser {
public void setLdapTemplate(LdapTemplate ldapTemplate);
public User getUser(String gid);
}
Any help would be greatly appreciated.
Thanks and br,
Elmar
-
Dec 29th, 2012, 01:20 PM
#2
Figured it out by my own. It was that well-known problem that in step scope interfaces have to be used for autowired beans instead of implementations. I replaced LdapTemplate by LdapOperations as type for the bean autowired to LdapTestUser and it worked fine!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules