embedded-ldap-junit icon indicating copy to clipboard operation
embedded-ldap-junit copied to clipboard

Replacement for @Rule in JUnit5

Open hazemkmammu opened this issue 4 years ago • 4 comments

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.

hazemkmammu avatar Jul 13 '20 12:07 hazemkmammu

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 avatar Oct 28 '20 15:10 bjansen

See PR #67

bjansen avatar Oct 28 '20 20:10 bjansen

+1, that would be awesome

fabiencelier avatar Nov 20 '20 16:11 fabiencelier

+1

Umit-Soylu avatar Jun 08 '21 09:06 Umit-Soylu