Skip to content

Commit

Permalink
Introduce the ability to inject context into @QuarkusIntegrationTest …
Browse files Browse the repository at this point in the history
…tests

Resolves: quarkusio#18854
  • Loading branch information
geoand committed Jul 20, 2021
1 parent a91b095 commit ccb8401
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
quarkus.mongodb.connection-string=mongodb://localhost:27017
quarkus.mongodb.write-concern.journal=false
quarkus.mongodb.database=test
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package io.quarkus.it.mongodb.rest.data.panache;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import io.quarkus.test.junit.NativeImageTest;
import io.quarkus.test.junit.QuarkusIntegrationTest;

@NativeImageTest
@QuarkusIntegrationTest
@DisabledOnOs(OS.WINDOWS)
class MongoDbRestDataPanacheIT extends MongoDbRestDataPanacheTest {

QuarkusIntegrationTest.Context context;

@Test
public void testDevServicesProperties() {
assertThat(context.devServicesProperties()).hasSize(1).containsKey("quarkus.mongodb.connection-string");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,10 @@
import org.junit.jupiter.api.condition.DisabledOnOs;
import org.junit.jupiter.api.condition.OS;

import io.quarkus.test.common.QuarkusTestResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.mongodb.MongoTestResource;
import io.restassured.response.Response;

@QuarkusTest
@QuarkusTestResource(MongoTestResource.class)
@DisabledOnOs(OS.WINDOWS)
class MongoDbRestDataPanacheTest {

Expand Down
5 changes: 5 additions & 0 deletions integration-tests/oidc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
<artifactId>awaitility</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<!-- Minimal test dependencies to *-deployment artifacts for consistent build order -->
<dependency>
<groupId>io.quarkus</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package io.quarkus.it.keycloak;

import io.quarkus.test.junit.NativeImageTest;
import static org.assertj.core.api.Assertions.assertThat;

@NativeImageTest
import org.junit.jupiter.api.Test;

import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class BearerTokenAuthorizationInGraalITCase extends BearerTokenAuthorizationTest {

QuarkusIntegrationTest.Context context;

@Test
public void testDevServicesProperties() {
assertThat(context.devServicesProperties()).isEmpty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static void startLauncher(ArtifactLauncher launcher, Map<String, String> additio
}
}

static Map<String, String> handleDevDb(ExtensionContext context) throws Exception {
static Map<String, String> handleDevServices(ExtensionContext context) throws Exception {
Class<?> requiredTestClass = context.getRequiredTestClass();
Path testClassLocation = getTestClassesLocation(requiredTestClass);
final Path appClassLocation = getAppClassLocationForTestLocation(testClassLocation.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import static io.quarkus.test.junit.IntegrationTestUtil.ensureNoInjectAnnotationIsUsed;
import static io.quarkus.test.junit.IntegrationTestUtil.getAdditionalTestResources;
import static io.quarkus.test.junit.IntegrationTestUtil.getSysPropsToRestore;
import static io.quarkus.test.junit.IntegrationTestUtil.handleDevDb;
import static io.quarkus.test.junit.IntegrationTestUtil.handleDevServices;
import static io.quarkus.test.junit.IntegrationTestUtil.startLauncher;

import java.util.HashMap;
Expand Down Expand Up @@ -110,7 +110,7 @@ private IntegrationTestExtensionState ensureStarted(ExtensionContext extensionCo

private IntegrationTestExtensionState doNativeStart(ExtensionContext context, Class<? extends QuarkusTestProfile> profile)
throws Throwable {
Map<String, String> devDbProps = handleDevDb(context);
Map<String, String> devServicesProps = handleDevServices(context);
quarkusTestProfile = profile;
currentJUnitTestClass = context.getRequiredTestClass();
TestResourceManager testResourceManager = null;
Expand All @@ -128,7 +128,7 @@ private IntegrationTestExtensionState doNativeStart(ExtensionContext context, Cl
hasPerTestResources = testResourceManager.hasPerTestResources();

Map<String, String> additionalProperties = new HashMap<>(testProfileAndProperties.properties);
additionalProperties.putAll(devDbProps);
additionalProperties.putAll(devServicesProps);
Map<String, String> resourceManagerProps = testResourceManager.start();
Map<String, String> old = new HashMap<>();
for (Map.Entry<String, String> i : resourceManagerProps.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.Map;

import org.junit.jupiter.api.extension.ExtendWith;

Expand Down Expand Up @@ -31,4 +32,17 @@
@ExtendWith({ DisabledOnIntegrationTestCondition.class, QuarkusTestExtension.class, QuarkusIntegrationTestExtension.class })
@Retention(RetentionPolicy.RUNTIME)
public @interface QuarkusIntegrationTest {

/**
* If used as a field of class annotated with {@link QuarkusIntegrationTest}, the field is populated
* with an implementation that allows accessing contextual test information
*/
interface Context {

/**
* Returns a map containing all the properties creates by potentially launched dev services.
* If no dev services where launched, the map will be empty.
*/
Map<String, String> devServicesProperties();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.quarkus.test.junit.IntegrationTestUtil.*;
import static io.quarkus.test.junit.IntegrationTestUtil.ensureNoInjectAnnotationIsUsed;

import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -41,6 +42,8 @@ public class QuarkusIntegrationTestExtension
private static Class<?> currentJUnitTestClass;
private static boolean hasPerTestResources;

private static Map<String, String> devServicesProps;

@Override
public void afterEach(ExtensionContext context) throws Exception {
if (!failedBoot) {
Expand Down Expand Up @@ -102,7 +105,7 @@ private IntegrationTestExtensionState ensureStarted(ExtensionContext extensionCo
private IntegrationTestExtensionState doProcessStart(Properties quarkusArtifactProperties,
Class<? extends QuarkusTestProfile> profile, ExtensionContext context)
throws Throwable {
Map<String, String> devDbProps = handleDevDb(context);
devServicesProps = handleDevServices(context);
quarkusTestProfile = profile;
currentJUnitTestClass = context.getRequiredTestClass();
TestResourceManager testResourceManager = null;
Expand All @@ -119,7 +122,7 @@ private IntegrationTestExtensionState doProcessStart(Properties quarkusArtifactP
hasPerTestResources = testResourceManager.hasPerTestResources();

Map<String, String> additionalProperties = new HashMap<>(testProfileAndProperties.properties);
additionalProperties.putAll(devDbProps);
additionalProperties.putAll(devServicesProps);
Map<String, String> resourceManagerProps = testResourceManager.start();
Map<String, String> old = new HashMap<>();
for (Map.Entry<String, String> i : resourceManagerProps.entrySet()) {
Expand Down Expand Up @@ -189,6 +192,30 @@ public void postProcessTestInstance(Object testInstance, ExtensionContext contex
ensureStarted(context);
if (!failedBoot) {
doProcessTestInstance(testInstance, context);
injectTestContext(testInstance);
}
}

private void injectTestContext(Object testInstance) {
Class<?> c = testInstance.getClass();
while (c != Object.class) {
for (Field f : c.getDeclaredFields()) {
if (f.getType().equals(QuarkusIntegrationTest.Context.class)) {
try {
Map<String, String> devServicesPropsCopy = devServicesProps.isEmpty() ? Collections.emptyMap()
: Collections.unmodifiableMap(devServicesProps);
QuarkusIntegrationTest.Context testContext = new DefaultQuarkusIntegrationTestContext(
devServicesPropsCopy);
f.setAccessible(true);
f.set(testInstance, testContext);
return;
} catch (Exception e) {
throw new RuntimeException("Unable to set field '" + f.getName()
+ "' with the proper test context", e);
}
}
}
c = c.getSuperclass();
}
}

Expand Down Expand Up @@ -229,4 +256,18 @@ public Class<?> testClass() {
return requiredTestClass;
}
}

private static class DefaultQuarkusIntegrationTestContext implements QuarkusIntegrationTest.Context {

private final Map<String, String> map;

private DefaultQuarkusIntegrationTestContext(Map<String, String> map) {
this.map = map;
}

@Override
public Map<String, String> devServicesProperties() {
return map;
}
}
}

0 comments on commit ccb8401

Please sign in to comment.