【发布时间】:2015-02-16 07:41:43
【问题描述】:
我正在尝试使用 Spring Ldap 设置嵌入式 ldap 以进行单元测试。但我需要为自定义对象类/属性定义使用自定义模式。如何使用 Spring Ldap 测试配置它 (LdapTestUtils?)
实际上,如果我运行测试,它会说我的自定义对象类“myOb”未在架构中定义,并显示以下消息:
org.springframework.ldap.UncategorizedLdapException: Failed to populate LDIF; nested exception is javax.naming.directory.NoSuchAttributeException: [LDAP: error code 16 - NO_SUCH_ATTRIBUTE: failed for Add Request :
...
: OID for name 'myOb' was not found within the OID registry]; remaining name 'cn=123456, ou=MyUser, o=company.com'
如果我从 ldif 评论 objectClass: myOb,则测试失败并返回空值(未读取属性)。
这是我的测试课:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = LdapConfiguration.class, loader = AnnotationConfigContextLoader.class)
public class LdapTest {
// Ldap port
private static final int LDAP_PORT = 18880;
// Base DN for test data
private static final LdapName baseName = LdapUtils.newLdapName("o=company.com");
@Autowired
LdapTemplate ldapTemplate;
@BeforeClass
public static void setupBeforeClass() {
LdapTestUtils.startEmbeddedServer(LDAP_PORT, baseName.toString(), "ldaptest");
// How to load schema definition ?
}
@AfterClass
public static void teardownAfterClass() throws Exception {
LdapTestUtils.shutdownEmbeddedServer();
}
@Before
public void setup() throws Exception {
LdapTestUtils.cleanAndSetup(ldapTemplate.getContextSource(), baseName, new ClassPathResource("ldap/test-users.ldif"));
}
@Test
public void testSearchLdap() throws Exception {
String myObId = ldapTemplate.lookup(LdapNameBuilder.newInstance("ou=MyUser, o=company.com").add("cn", "123456").build(), new AbstractContextMapper<String>() {
@Override
protected String doMapFromContext(DirContextOperations ctx) {
return ctx.getStringAttribute("myObId"); // custom type
}
});
Assert.assertNotNull(myObId); // myObId is null if I comment `objectClass: myOb` !
}
}
还有我的 ldif:
dn: ou=MyUser, o=company.com
ou: User
description: MyUser
objectClass: top
objectClass: organizationalunit
dn: cn=123456, ou=MyUser, o=company.com
objectClass: top
objectClass: person
objectClass: myOb
cn: 123456
sn: 823456
myObId: TEST
【问题讨论】:
-
嗨,我也面临同样的问题。您是否设法解决了它,还是最终使用了下面建议的解决方法?
-
是的,我正在使用下面建议的
InMemoryDirectoryServer服务器解决方案 -
好的,谢谢,看来我也得这么做了……
标签: java spring unit-testing ldap spring-ldap