有人可以帮我将 EmbeddedLdapRule 移植到 JUnit5 中吗?我尝试使用 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();
我也有兴趣知道这个问题的解决方案?,
我也有同样的问题更新 @Rule 到 junit5 的 @RegisterExtension,
但问题是,在 EmbeddedLdapRule Lib 中,它依赖于 Junit4: 导入 org.junit.rules.TestRule.
我发现没有办法排除和替换这个 Junit 依赖项。