Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PR] - HelidonTest doesn't start container properly with TestInstance.Lifecycle.PER_CLASS #4663 #4865

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -363,6 +365,16 @@ public <T> T interceptTestClassConstructor(Invocation<T> 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);
}

Expand Down