This is an automated email from the ASF dual-hosted git repository. asf-gitbox-commits pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/deltaspike.git
commit 0135d39c672aa96f13e2c37c571aac369fda88bb Author: Mark Struberg <[email protected]> AuthorDate: Sun May 10 18:25:39 2026 +0200 DELTASPIKE-1366 enable junit5 test run and fix lifecycle this was a bit tricky. Due to the mixture of junit4 and 5 dependencies, our tests did not get picked up. I also made the CdiTestExtension implement the TestInstanceFactory to create the test instances. This mechanism surely needs further improvements --- .../testcontrol5/api/junit/CdiTestExtension.java | 55 ++++++++++++++++++++-- deltaspike/modules/test-control5/impl/pom.xml | 47 +++++++++++++++++- .../RequestAndSessionScopePerTestMethodTest.java | 2 - 3 files changed, 97 insertions(+), 7 deletions(-) diff --git a/deltaspike/modules/test-control5/api/src/main/java/org/apache/deltaspike/testcontrol5/api/junit/CdiTestExtension.java b/deltaspike/modules/test-control5/api/src/main/java/org/apache/deltaspike/testcontrol5/api/junit/CdiTestExtension.java index 49aa23ea2..685d17c4d 100644 --- a/deltaspike/modules/test-control5/api/src/main/java/org/apache/deltaspike/testcontrol5/api/junit/CdiTestExtension.java +++ b/deltaspike/modules/test-control5/api/src/main/java/org/apache/deltaspike/testcontrol5/api/junit/CdiTestExtension.java @@ -18,10 +18,14 @@ */ package org.apache.deltaspike.testcontrol5.api.junit; +import jakarta.enterprise.context.spi.CreationalContext; +import jakarta.enterprise.inject.spi.Bean; +import jakarta.enterprise.inject.spi.BeanManager; import org.apache.deltaspike.cdise.api.CdiContainer; import org.apache.deltaspike.cdise.api.CdiContainerLoader; import org.apache.deltaspike.cdise.api.ContextControl; import org.apache.deltaspike.core.api.projectstage.ProjectStage; +import org.apache.deltaspike.core.api.provider.BeanManagerProvider; import org.apache.deltaspike.core.api.provider.BeanProvider; import org.apache.deltaspike.core.util.ExceptionUtils; import org.apache.deltaspike.core.util.ProjectStageProducer; @@ -45,8 +49,12 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.enterprise.context.RequestScoped; import jakarta.enterprise.context.SessionScoped; import jakarta.inject.Singleton; +import org.junit.jupiter.api.extension.TestInstanceFactory; +import org.junit.jupiter.api.extension.TestInstanceFactoryContext; +import org.junit.jupiter.api.extension.TestInstantiationException; import java.lang.annotation.Annotation; +import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; @@ -54,7 +62,6 @@ import java.util.Comparator; import java.util.List; import java.util.Set; import java.util.Stack; -import java.util.concurrent.CopyOnWriteArraySet; import java.util.logging.Handler; import java.util.logging.Level; import java.util.logging.Logger; @@ -64,14 +71,14 @@ import java.util.logging.Logger; * A JUnit 5 extension to start up with a CDI or embedded JavaEE container. */ public class CdiTestExtension implements BeforeAllCallback, AfterAllCallback, - BeforeEachCallback, AfterEachCallback, ParameterResolver + BeforeEachCallback, AfterEachCallback, ParameterResolver, + TestInstanceFactory { private static final Logger LOGGER = Logger.getLogger(CdiTestExtension.class.getName()); private static final boolean USE_TEST_CLASS_AS_CDI_BEAN; private static final boolean ALLOW_INJECTION_POINT_MANIPULATION; - private static Set<Integer> extensionIdentities = new CopyOnWriteArraySet<Integer>(); static { @@ -91,6 +98,43 @@ public class CdiTestExtension implements BeforeAllCallback, AfterAllCallback, { } + /** + * we need to deal with the creation itself as CDI tests are more like {@code @TestInstance(Lifecycle.PER_CLASS)}. + * + * @return the new test instance. + * @throws TestInstantiationException + */ + @Override + public Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext) throws TestInstantiationException + { + final Class<?> testClass = factoryContext.getTestClass(); + BeanManager beanManager = BeanManagerProvider.getInstance().getBeanManager(); + Set<Bean<?>> beans = beanManager.getBeans(testClass); + if (beans.isEmpty()) + { + try + { + Object instance = testClass.getDeclaredConstructor().newInstance(); + BeanProvider.injectFields(instance); + return instance; + } + catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) + { + throw new RuntimeException(e); + } + } + else + { + Bean<Object> bean = (Bean<Object>) beanManager.resolve(beans); + + CreationalContext<Object> creationalContext = beanManager.createCreationalContext(bean); + + Object instance = beanManager.getReference(bean, testClass, creationalContext); + + return instance; + } + } + @Override public void beforeEach(ExtensionContext extensionContext) throws Exception { @@ -169,6 +213,7 @@ public class CdiTestExtension implements BeforeAllCallback, AfterAllCallback, }); } + this.testContext.applyBeforeClassConfig(extensionContext.getTestClass().orElseThrow()); } @@ -179,6 +224,10 @@ public class CdiTestExtension implements BeforeAllCallback, AfterAllCallback, { this.testContext.applyAfterClassConfig(); } + + // TODO destroy all injected beans + // this might need a namespace storage in the context? + } @Override diff --git a/deltaspike/modules/test-control5/impl/pom.xml b/deltaspike/modules/test-control5/impl/pom.xml index a9cbed09c..682c1ea73 100644 --- a/deltaspike/modules/test-control5/impl/pom.xml +++ b/deltaspike/modules/test-control5/impl/pom.xml @@ -43,6 +43,18 @@ </deltaspike.osgi.import> </properties> + <dependencyManagement> + <dependencies> + <dependency> + <groupId>org.junit</groupId> + <artifactId>junit-bom</artifactId> + <version>5.10.2</version> + <type>pom</type> + <scope>import</scope> + </dependency> + </dependencies> + </dependencyManagement> + <dependencies> <dependency> <groupId>org.apache.deltaspike.modules</groupId> @@ -66,8 +78,21 @@ <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> - <version>${junit5.version}</version> - <scope>provided</scope> + <scope>compile</scope> + </dependency> + + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <scope>test</scope> + </dependency> + + <!-- Force launcher version to match surefire's bundled platform version --> + <dependency> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-launcher</artifactId> + <version>1.10.2</version> + <scope>test</scope> </dependency> <dependency> @@ -92,8 +117,26 @@ <version>${project.version}</version> <scope>runtime</scope> </dependency> + + </dependencies> + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <dependencies> + <dependency> + <groupId>org.apache.maven.surefire</groupId> + <artifactId>surefire-junit-platform</artifactId> + <version>${maven.surefire.plugin.version}</version> + </dependency> + </dependencies> + </plugin> + </plugins> + </build> + <profiles> <profile> diff --git a/deltaspike/modules/test-control5/impl/src/test/java/org/apache/deltaspike/test/testcontrol5/uc001/RequestAndSessionScopePerTestMethodTest.java b/deltaspike/modules/test-control5/impl/src/test/java/org/apache/deltaspike/test/testcontrol5/uc001/RequestAndSessionScopePerTestMethodTest.java index a5ed71829..3000c28ac 100644 --- a/deltaspike/modules/test-control5/impl/src/test/java/org/apache/deltaspike/test/testcontrol5/uc001/RequestAndSessionScopePerTestMethodTest.java +++ b/deltaspike/modules/test-control5/impl/src/test/java/org/apache/deltaspike/test/testcontrol5/uc001/RequestAndSessionScopePerTestMethodTest.java @@ -26,7 +26,6 @@ import org.apache.deltaspike.test.testcontrol5.shared.TestUtils; import org.apache.deltaspike.testcontrol5.api.junit.CdiTestExtension; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -35,7 +34,6 @@ import jakarta.inject.Inject; import static org.junit.jupiter.api.Assertions.assertEquals; //Usually NOT needed! Currently only needed due to our arquillian-setup -@Tag("SeCategory") @ExtendWith(CdiTestExtension.class) //starts container once and one session + request per test-method //implicitly annotated with @TestControl without the default-scope settings
