Skip to content

Commit

Permalink
Properly resolve @TestProfile when using nested tests in base classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Sgitario authored and michelle-purcell committed Aug 1, 2023
1 parent cc2ebbc commit a7f631d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.it.main;

import static org.hamcrest.Matchers.is;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;

import io.restassured.RestAssured;

public abstract class CommonNestedTest {

public String defaultProfile() {
return "Hello";
}

@Nested
class NestedTests {
@Test
public void testProfileFromNested() {
RestAssured.when()
.get("/greeting/Stu")
.then()
.statusCode(200)
.body(is(defaultProfile() + " Stu"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.quarkus.it.main;

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;

@QuarkusTest
@TestProfile(QuarkusTestNestedWithTestProfileTestCase.OuterProfile.class)
public class QuarkusTestNested1WithCommonCaseTest extends CommonNestedTest {

@Override
public String defaultProfile() {
return "OuterProfile";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.it.main;

import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
public class QuarkusTestNested2WithCommonCaseTest extends CommonNestedTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import jakarta.enterprise.inject.Alternative;

import org.jboss.jandex.Index;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.extension.ExtensionContext;

import io.quarkus.bootstrap.BootstrapConstants;
Expand Down Expand Up @@ -213,7 +215,42 @@ private ApplicationModel getGradleAppModelForIDE(Path projectRoot) throws IOExce
}

protected Class<? extends QuarkusTestProfile> getQuarkusTestProfile(ExtensionContext extensionContext) {
Class<?> testClass = extensionContext.getRequiredTestClass();
// If the current class or any enclosing class in its hierarchy is annotated with `@TestProfile`.
Class<? extends QuarkusTestProfile> testProfile = findTestProfileAnnotation(extensionContext.getRequiredTestClass());
if (testProfile != null) {
return testProfile;
}

// Otherwise, if the current class is annotated with `@Nested`:
if (extensionContext.getRequiredTestClass().isAnnotationPresent(Nested.class)) {
// let's try to find the `@TestProfile` from the enclosing classes:
testProfile = findTestProfileAnnotation(extensionContext.getRequiredTestClass().getEnclosingClass());
if (testProfile != null) {
return testProfile;
}

// if not found, let's try the parents
Optional<ExtensionContext> parentContext = extensionContext.getParent();
while (parentContext.isPresent()) {
ExtensionContext currentExtensionContext = parentContext.get();
if (currentExtensionContext.getTestClass().isEmpty()) {
break;
}

testProfile = findTestProfileAnnotation(currentExtensionContext.getTestClass().get());
if (testProfile != null) {
return testProfile;
}

parentContext = currentExtensionContext.getParent();
}
}

return null;
}

private Class<? extends QuarkusTestProfile> findTestProfileAnnotation(Class<?> clazz) {
Class<?> testClass = clazz;
while (testClass != null) {
TestProfile annotation = testClass.getAnnotation(TestProfile.class);
if (annotation != null) {
Expand Down

0 comments on commit a7f631d

Please sign in to comment.