apache - 使用 JUnit5 的 EmbeddedLdapRule
问题描述
有人可以帮我在 JUnit5 中移植 EmbeddedLdapRule 吗?我尝试使用 JUnit4,它运行良好。但是 Apached LDAP-API 无法在 JUnit5 中连接。
基本上,我喜欢启动嵌入式 ldap 并使用 Apached LDAP-API 连接到它以进行单元测试。
class EmbeddedLDAPServer implements BeforeEachCallback {
private final String host;
private final int port;
private final String adminPassword;
private final String adminUser;
private final String dsn;
private final String ldifFilename;
public EmbeddedLdapRule embeddedLdapRule;
public EmbeddedLDAPServer(String host,
int port,
String adminUser,
String adminPassword,
String dsn,
String ldifFileName) {
this.host = host;
this.port = port;
this.adminUser = adminUser;
this.adminPassword = adminPassword;
this.dsn = dsn;
this.ldifFilename = ldifFileName;
}
@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {
System.out.println("dsn : " + dsn);
System.out.println("port: " + port);
System.out.println("ldif: " + ldifFilename);
embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance()
.usingDomainDsn(dsn)
.bindingToAddress(host)
.bindingToPort(port)
.usingBindDSN(adminUser)
.usingBindCredentials(adminPassword)
.importingLdifs(ldifFilename)
.build();
}
}
注册:
@RegisterExtension
static EmbeddedLDAPServer embeddedLDAPServer = new EmbeddedLDAPServer(LDAP_HOST,
LDAP_PORT,
ADMIN_DN,
ADMIN_PASSWORD,
BASEDN,
"users.ldif");
并且尝试连接失败:
@BeforeEach
public void setup() throws LdapException {
System.out.println("setup()");
LdapConnectionConfig config = new LdapConnectionConfig();
config.setLdapHost(LDAP_HOST);
config.setLdapPort(LDAP_PORT);
config.setName(ADMIN_DN);
config.setCredentials(ADMIN_PASSWORD);
connection = new LdapNetworkConnection(config);
connection.bind();
Assertions.assertTrue(connection.isConnected());
Assertions.assertTrue(connection.isAuthenticated());
}
Adding the JUnit4 @Rule
@Rule
public EmbeddedLdapRule embeddedLdapRule = EmbeddedLdapRuleBuilder.newInstance()
.usingDomainDsn(BASEDN)
.bindingToAddress(LDAP_HOST)
.bindingToPort(LDAP_PORT)
.usingBindDSN(ADMIN_DN)
.usingBindCredentials(ADMIN_PASS)
.importingLdifs("users.ldif")
.build();
解决方案
推荐阅读
- javascript - 在哪里可以找到 EJS-Lint 输出,它应该是什么样的?
- r - 如何删除 ggplot2 填充类别中的模式?
- java - Java 代码中的 Kotlin List 与 java.util.List 泛型类型
- python - 为什么在使用带有 urllib3 的 HTTPS 代理时看到 InvalidProxyConfigurationWarning?
- android - 如何从 Firestore 地图字段中添加/删除?
- c# - Xamarin.Forms - iOS RegisteredForRemoteNotifications 没有被调用
- xml - 使用 DTD 可以指定元素出现的确切数量吗?
- fusionauth - 如何限制用户登录/身份验证访问单个租户中的特定应用程序?
- javascript - 如何从 NodeJS 中的 blob 中获取 WAV 文件
- python - 在给定多个条件的数组中查找一个值