Actually with the BeforeAllCallback we can get the test class from the
ExtensionContext and set the static fields via reflection:

public class BeforeAllInjector implements BeforeAllCallback
{
    @Override
    public void beforeAll( ExtensionContext context ) throws Exception
    {
        Class<?> testClass = context.getTestClass().get();
        testClass.getField( "service" ).set( null, new
DefaultDirectoryService() );
        testClass.getField( "ldapServer" ).set( null, new LdapServer() );
    }
}


On 4/4/21 1:49 PM, Stefan Seelmann wrote:
> Instead of BeforeAll it seems we can use TestInstancePostProcessor which
> postProcessTestInstance() method signature includes the test instance:
> 
> public class BeforeAllInjector implements TestInstancePostProcessor
> {
>     @Override
>     public void postProcessTestInstance( Object testInstance,
> ExtensionContext context ) throws Exception
>     {
>         AbstractLdapTestUnit test = (AbstractLdapTestUnit) testInstance;
>         test.service = new DefaultDirectoryService();
>         test.ldapServer = new LdapServer();
>     }
> }
> 
> 
> In the BeforeEach case the ExtensionContext provides a reference to the
> test instance:
> 
> public class BeforeEachInjector implements BeforeEachCallback
> {
>     @Override
>     public void beforeEach( ExtensionContext context ) throws Exception
>     {
>         AbstractLdapTestUnit test = context.getTestInstance().map(
> AbstractLdapTestUnit.class::cast ).get();
>         test.service = new DefaultDirectoryService();
>         test.ldapServer = new LdapServer();
>     }
> }
> 
> 
> 
> 
> 
> On 4/4/21 11:47 AM, Emmanuel Lécharny wrote:
>> Hi !
>>
>> I'm fighting with a piece of our test framework that worked well with
>> Junit4, not so much with Junit5.
>>
>> In Junit4, we were using Rule and ClassRule to declare some
>> DirectoryService used in tests (typically, if we want to test some
>> features, we start with a DS creation that will be visible by all the
>> tests. This is what we do with ClassRule. If we want a specific test to
>> declare a new DS, we use a Rule for that: this is for instance what we
>> do to check replication, where we need 2 DS).
>>
>> Anyway, in Junit5, Rule and ClassRule has been removed, and we have to
>> implement Extensions, which are interface containing declaration for
>> those methods :
>>
>> BeforeEachCallback,
>> AfterEachCallback,
>> BeforeAllCallback,
>> AfterAllCallback
>>
>> The logic is pretty simlple: those callbacks get called before the first
>> test, before each test, after each test and after all tests.
>>
>> Nothing fancy, and it's quite smart.
>>
>> Now, the pb I have is that I have to declare the DS instance in the
>> BeforeAll callback, and sometime declare a DS in a BeforeEach callback
>> (but not necessarilly). The trouble is that those extensions are not
>> visible by the tests, so I can't use the instance.
>>
>> I can foresee a hack: creating a static class that will contain the
>> instance, feed it in the BeforeEach and BeforeAll callbacks, and make
>> the fields visible.
>>
>> It's ugly...
>>
>> I have trie to play with the ParameterResolver, which is a way to start
>> Parameterized tests (and so pass a parameter to a test), but with not
>> much success so far.
>>
>> The second issue is that I don't want to create a DS instance everytime
>> I call BeforeEach. I don't know how to do that.
>>
>> If any of you have soime suggestion, that would be very appreciate !
>>
>> Thanks !
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to