zapodot / embedded-ldap-junit

A JUnit rule for running an embedded LDAP server in your JUnit test
MIT License
34 stars 12 forks source link

Replacement for @Rule in JUnit5 #57

Open hazemkmammu opened 4 years ago

hazemkmammu commented 4 years ago

Please provide hints on using the library with JUnit5. JUnit5 no longer supports the @Rule annotation and it is replaced by Extension. I think If we can do the initialization part directly using the library API from @BeforeAll, we can replace what @Rule essentially does.

bjansen commented 4 years ago

I have a working PoC that looks like this:

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.zapodot.junit.ldap.junit5.EmbeddedLdapExtension;
import org.zapodot.junit.ldap.junit5.EmbeddedLdapExtensionBuilder;

class JUnit5Test {

    @RegisterExtension
    static EmbeddedLdapExtension ldapServer = EmbeddedLdapExtensionBuilder.newInstance()
            .usingDomainDsn("O=foo,O=bar,O=fr")
            .withSchema("custom.ldif")
            .importingLdifs("annuaire.ldif")
            .usingBindDSN("...")
            .usingBindCredentials("...")
            .build();

    @Test
    void hello() {
        System.out.println(ldapServer.embeddedServerPort());
    }
}

A big chunk of code was copy/pasted, but most of it it not directly related to JUnit, so I think I'll extract it as common abstract classes.

The extension would have the same API as the JUnit 4 rule, and could be used on static (@BeforeAll/@AfterAll) or instance (@BeforeEach/@AfterEach) fields.

bjansen commented 4 years ago

See PR #67

fabiencelier commented 4 years ago

+1, that would be awesome

Umit-Soylu commented 3 years ago

+1