Re: Issue with Junit5 test framework. Any idea?
Hi! FTR, I now have all tests passing on core-integration (and every module before this one). I have some failure on Protocol Kerberos tests, but this is not related. Keep going... On 04/04/2021 11:47, 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 ! -- *Emmanuel Lécharny - CTO* 205 Promenade des Anglais – 06200 NICE T. +33 (0)4 89 97 36 50 P. +33 (0)6 08 33 32 61 emmanuel.lecha...@busit.com https://www.busit.com/ - To unsubscribe, e-mail: dev-unsubscr...@directory.apache.org For additional commands, e-mail: dev-h...@directory.apache.org
Re: Issue with Junit5 test framework. Any idea?
Okie, super, I have my first couple of tests working ! There is a lot of refactoring to do yet, but at least, I'm not blocked anymore :-) Thanks Stefan, you were super helpful ! On 05/04/2021 06:42, Emmanuel Lécharny wrote: On 04/04/2021 23:17, Emmanuel Lécharny wrote: Hmmm, I think there must be a side effect. The following line : testClass.getField( "service" )... can't return the reference of the service field, which is not present in the LdapConnectionTest class, but is present in the AbstractLdapTestUnit parent class. And getField() does not return fields from super classes... And I'm plain wrong :/ And all I had to do to get the test passing was to declare my DirectoryService fields as static. Also not declaring those fields as private because when you call a getDeclaredField, there you don't get the inherited fields. Thanks Stefan, it took me quite some time to understand what was wrong in my test ! -- *Emmanuel Lécharny - CTO* 205 Promenade des Anglais – 06200 NICE T. +33 (0)4 89 97 36 50 P. +33 (0)6 08 33 32 61 emmanuel.lecha...@busit.com https://www.busit.com/ - To unsubscribe, e-mail: dev-unsubscr...@directory.apache.org For additional commands, e-mail: dev-h...@directory.apache.org
Re: Issue with Junit5 test framework. Any idea?
On 04/04/2021 23:17, Emmanuel Lécharny wrote: Hmmm, I think there must be a side effect. The following line : testClass.getField( "service" )... can't return the reference of the service field, which is not present in the LdapConnectionTest class, but is present in the AbstractLdapTestUnit parent class. And getField() does not return fields from super classes... And I'm plain wrong :/ And all I had to do to get the test passing was to declare my DirectoryService fields as static. Also not declaring those fields as private because when you call a getDeclaredField, there you don't get the inherited fields. Thanks Stefan, it took me quite some time to understand what was wrong in my test ! -- *Emmanuel Lécharny - CTO* 205 Promenade des Anglais – 06200 NICE T. +33 (0)4 89 97 36 50 P. +33 (0)6 08 33 32 61 emmanuel.lecha...@busit.com https://www.busit.com/ - To unsubscribe, e-mail: dev-unsubscr...@directory.apache.org For additional commands, e-mail: dev-h...@directory.apache.org
Re: Issue with Junit5 test framework. Any idea?
I would add that in my case, when the beforeAll callback is called, we don't have yet an instance of the test class, which leads to a NPE later when we try to set the field's value - which is expected -. FTR here is the extension I'm using: /* * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.directory.server.core.integ; import java.lang.reflect.AnnotatedElement; import java.lang.reflect.Field; import org.apache.directory.api.util.FileUtils; import org.apache.directory.server.core.annotations.CreateDS; import org.apache.directory.server.core.api.DirectoryService; import org.apache.directory.server.core.api.changelog.Tag; import org.apache.directory.server.core.factory.DSAnnotationProcessor; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class CreateDSTestExtension implements BeforeEachCallback, AfterEachCallback, BeforeAllCallback, AfterAllCallback { private static final Logger LOG = LoggerFactory.getLogger( CreateDSTestExtension.class ); private DirectoryService directoryService; private CreateDSTestExtension outerCreateDsRule; public CreateDSTestExtension() { } @Override public void afterAll( ExtensionContext context ) throws Exception { } @Override public void beforeAll( ExtensionContext context ) throws Exception { // Don't run the test if the @Disabled annotation is used if ( context.getTestClass().get().getAnnotation( Disabled.class ) != null ) { return; } AnnotatedElement classAnnotation = context.getTestClass().get(); CreateDS createDs = classAnnotation.getAnnotation( CreateDS.class ); if ( createDs == null ) { DirectoryService directoryService = getDirectoryService(); if ( ( directoryService != null ) && directoryService.getChangeLog().isEnabled() ) { Tag tag = directoryService.getChangeLog().tag(); DSAnnotationProcessor.applyLdifs( classAnnotation, classAnnotation.getClass().getName(), directoryService ); LOG.debug( "Tagged change log: {}", tag ); } else { LOG.trace( "no @CreateDS and no outer @CreateDS on: {}", classAnnotation.getClass().getName() ); } } else { LOG.trace( "Creating directory service" ); directoryService = DSAnnotationProcessor.getDirectoryService( createDs ); DSAnnotationProcessor.applyLdifs( classAnnotation, classAnnotation.getClass().getName(), directoryService ); } Class testClass = context.getTestClass().get(); Field classDirectoryService = testClass.getDeclaredField( "classDirectoryService" ); classDirectoryService.setAccessible( true ); try { classDirectoryService.set( null, directoryService ); } catch ( Exception e ) { e.printStackTrace(); // throw e; } } ... and in my test I start with: @ExtendWith( CreateDSTestExtension.class ) On 04/04/2021 23:17, Emmanuel Lécharny wrote: Hmmm, I think there must be a side effect. The following line : testClass.getField( "service" )... can't return the reference of the service field, which is not present in the LdapConnectionTest class, but is present in the AbstractLdapTestUnit parent class. And getField() does not return fields from super classes... There is something weird. On 04/04/2021 19:17, Stefan Seelmann wrote: 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(
Re: Issue with Junit5 test framework. Any idea?
Hmmm, I think there must be a side effect. The following line : testClass.getField( "service" )... can't return the reference of the service field, which is not present in the LdapConnectionTest class, but is present in the AbstractLdapTestUnit parent class. And getField() does not return fields from super classes... There is something weird. On 04/04/2021 19:17, Stefan Seelmann wrote: 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:
Re: Issue with Junit5 test framework. Any idea?
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:
Re: Issue with Junit5 test framework. Any idea?
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 -- *Emmanuel Lécharny - CTO* 205 Promenade des Anglais – 06200 NICE T. +33 (0)4 89 97 36 50 P. +33 (0)6 08 33 32 61 emmanuel.lecha...@busit.com https://www.busit.com/ - To unsubscribe, e-mail: dev-unsubscr...@directory.apache.org For additional commands, e-mail: dev-h...@directory.apache.org
Re: Issue with Junit5 test framework. Any idea?
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 -- *Emmanuel Lécharny - CTO* 205 Promenade des Anglais – 06200 NICE T. +33 (0)4 89 97 36 50 P. +33 (0)6 08 33 32 61 emmanuel.lecha...@busit.com https://www.busit.com/ - To unsubscribe, e-mail: dev-unsubscr...@directory.apache.org For additional commands, e-mail: dev-h...@directory.apache.org
Re: Issue with Junit5 test framework. Any idea?
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
Re: Issue with Junit5 test framework. Any idea?
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