diff --git a/microprofile/tests/junit5/src/main/java/io/helidon/microprofile/tests/junit5/HelidonJunitExtension.java b/microprofile/tests/junit5/src/main/java/io/helidon/microprofile/tests/junit5/HelidonJunitExtension.java index 1a3fca32c3b..62636ff4fea 100644 --- a/microprofile/tests/junit5/src/main/java/io/helidon/microprofile/tests/junit5/HelidonJunitExtension.java +++ b/microprofile/tests/junit5/src/main/java/io/helidon/microprofile/tests/junit5/HelidonJunitExtension.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 2022 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -55,6 +55,7 @@ import org.eclipse.microprofile.config.spi.ConfigProviderResolver; import org.eclipse.microprofile.config.spi.ConfigSource; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.AfterAllCallback; import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.BeforeAllCallback; @@ -204,6 +205,7 @@ public void afterEach(ExtensionContext context) throws Exception { } private void validatePerClass() { + Method[] methods = testClass.getMethods(); for (Method method : methods) { if (method.getAnnotation(Test.class) != null) { @@ -363,6 +365,16 @@ public T interceptTestClassConstructor(Invocation invocation, // we need to start container before the test class is instantiated, to honor @BeforeAll that // creates a custom MP config if (container == null) { + + // at this early stage the class should be checked whether it is annotated with + // @TestInstance(TestInstance.Lifecycle.PER_CLASS) to start correctly the container + TestInstance testClassAnnotation = testClass.getAnnotation(TestInstance.class); + if (testClassAnnotation != null && testClassAnnotation.value().equals(TestInstance.Lifecycle.PER_CLASS)){ + throw new RuntimeException("When a class is annotated with @HelidonTest, " + + "it is not compatible with @TestInstance(TestInstance.Lifecycle.PER_CLASS)" + + "annotation, as it is a Singleton CDI Bean."); + } + startContainer(classLevelBeans, classLevelExtensions, classLevelDisableDiscovery); }