Here is a quick and dirty demo which "works on my machine" ;)

https://github.com/apache/directory-server/commit/d10cf254dca9317b54dd39489ed249098a984436

On 4/4/21 6:56 PM, Emmanuel Lécharny wrote:
> Sadly, that does not work :/
> 
> The test class is not yet existing when we call testClass.getField(
> "service" ).set( null, new DefaultDirectoryService() )
> 
> so I get a NPE on the set.
> 
> There seems to be a internal Junit5 mechanism that allow you to get the
> field to set but you can't inject some value into it...
> 
> 
> On 04/04/2021 17:55, Emmanuel Lécharny wrote:
>> Ah, cool, have'nt thought about this solution.
>>
>> Will try that !
>>
>> Thanks Stefan!
>>
>> On 04/04/2021 14:00, Stefan Seelmann wrote:
>>> 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: dev-unsubscr...@directory.apache.org
>>>> For additional commands, e-mail: dev-h...@directory.apache.org
>>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscr...@directory.apache.org
>>> For additional commands, e-mail: dev-h...@directory.apache.org
>>>
>>
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@directory.apache.org
For additional commands, e-mail: dev-h...@directory.apache.org

Reply via email to