Skip to content

Commit

Permalink
Merge pull request #24935 from Sgitario/fix_test_profile_nested
Browse files Browse the repository at this point in the history
Support @testprofile when using @nested tests
  • Loading branch information
geoand authored Apr 15, 2022
2 parents a7b719a + 8d3ed60 commit 3f8f3e9
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package io.quarkus.it.main;

import static org.hamcrest.Matchers.is;

import java.util.Collections;
import java.util.Map;

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

import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.QuarkusTestProfile;
import io.quarkus.test.junit.TestProfile;
import io.restassured.RestAssured;

@QuarkusTest
@Tag("nested")
@TestProfile(QuarkusTestNestedWithTestProfileTestCase.OuterProfile.class)
public class QuarkusTestNestedWithTestProfileTestCase {

private static final int TEST_PORT_FROM_PROFILE = 7777;

@Nested
class NestedCase {

@Test
void testProfileFromNested() {
Assertions.assertEquals(TEST_PORT_FROM_PROFILE, RestAssured.port);
RestAssured.when()
.get("/greeting/Stu")
.then()
.statusCode(200)
.body(is("OuterProfile Stu"));
}
}

@Nested
@TestProfile(QuarkusTestNestedWithTestProfileTestCase.ModernEnglishProfile.class)
class ModernEnglishCase {

@Test
void testProfileFromNested() {
RestAssured.when()
.get("/greeting/Stu")
.then()
.statusCode(200)
.body(is("Hey Stu"));
}
}

public static class OuterProfile implements QuarkusTestProfile {

@Override
public Map<String, String> getConfigOverrides() {
return Collections.singletonMap("quarkus.http.test-port", "" + TEST_PORT_FROM_PROFILE);
}

@Override
public String[] commandLineParameters() {
return new String[] { "OuterProfile" };
}

@Override
public boolean runMainMethod() {
return true;
}
}

public static class ModernEnglishProfile implements QuarkusTestProfile {

@Override
public String[] commandLineParameters() {
return new String[] { "Hey" };
}

@Override
public boolean runMainMethod() {
return true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,17 @@ protected PrepareResult createAugmentor(ExtensionContext context, Class<? extend
}

protected Class<? extends QuarkusTestProfile> getQuarkusTestProfile(ExtensionContext extensionContext) {
TestProfile annotation = extensionContext.getRequiredTestClass().getAnnotation(TestProfile.class);
Class<? extends QuarkusTestProfile> selectedProfile = null;
if (annotation != null) {
selectedProfile = annotation.value();
Class<?> testClass = extensionContext.getRequiredTestClass();
while (testClass != null) {
TestProfile annotation = testClass.getAnnotation(TestProfile.class);
if (annotation != null) {
return annotation.value();
}

testClass = testClass.getEnclosingClass();
}
return selectedProfile;

return null;
}

protected static boolean hasPerTestResources(ExtensionContext extensionContext) {
Expand Down

0 comments on commit 3f8f3e9

Please sign in to comment.