首页 > 解决方案 > 使用 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();


标签: apacheldapjunit5

解决方案


推荐阅读