Skip to content

Commit

Permalink
Add support for injection into @QuarkusTest
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Feb 28, 2019
1 parent 93aaed1 commit 8680597
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.quarkus.deployment.test;

public interface TestResourceProvider {

void inject(Object test);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.quarkus.arc.deployment;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.enterprise.context.spi.CreationalContext;
import javax.enterprise.inject.spi.Bean;
import javax.enterprise.inject.spi.BeanManager;
import javax.inject.Inject;
import javax.inject.Qualifier;

import io.quarkus.arc.Arc;
import io.quarkus.deployment.test.TestResourceProvider;

public class ArcTestResourceProvider implements TestResourceProvider {

@Override
public void inject(Object test) {
Class<?> c = test.getClass();
while (c != Object.class) {
for (Field f : c.getDeclaredFields()) {
if (f.isAnnotationPresent(Inject.class)) {
BeanManager beanManager = Arc.container().beanManager();
List<Annotation> qualifiers = new ArrayList<>();
for (Annotation a : f.getAnnotations()) {
if (a.annotationType().isAnnotationPresent(Qualifier.class)) {
qualifiers.add(a);
}
}
Set<Bean<?>> beans = beanManager.getBeans(f.getType(),
qualifiers.toArray(new Annotation[qualifiers.size()]));
Bean<?> bean = beanManager.resolve(beans);
CreationalContext<?> ctx = beanManager.createCreationalContext(bean);
Object instance = beanManager.getReference(bean, f.getType(), ctx);
f.setAccessible(true);
try {
f.set(test, instance);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
c = c.getSuperclass();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.quarkus.arc.deployment.ArcTestResourceProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright 2018 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.quarkus.example.test;

import javax.inject.Inject;

import org.eclipse.microprofile.health.Health;
import org.eclipse.microprofile.health.HealthCheckResponse;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;
import org.wildfly.common.Assert;

import io.quarkus.example.health.SimpleHealthCheck;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.SubstrateTest;

@QuarkusTest
public class HealthCheckTestCase {

@Inject
@Health
SimpleHealthCheck checkks;

@Test
public void testInjection() {
Assumptions.assumeFalse(getClass().isAnnotationPresent(SubstrateTest.class));
Assert.assertTrue(checkks.call().getState() == HealthCheckResponse.State.UP);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.test.common;

import java.util.ArrayList;
import java.util.List;
import java.util.ServiceLoader;

import io.quarkus.deployment.test.TestResourceProvider;

public class TestInjectionManager {

private static final List<TestResourceProvider> resourceProviders = new ArrayList<>();

static {
for (TestResourceProvider i : ServiceLoader.load(TestResourceProvider.class)) {
resourceProviders.add(i);
}
}

public static void inject(Object test) {
for (TestResourceProvider i : resourceProviders) {
i.inject(test);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Enumeration;
Expand Down Expand Up @@ -52,6 +51,7 @@
import io.quarkus.runtime.LaunchMode;
import io.quarkus.test.common.NativeImageLauncher;
import io.quarkus.test.common.RestAssuredURLManager;
import io.quarkus.test.common.TestInjectionManager;
import io.quarkus.test.common.TestResourceManager;
import io.quarkus.test.common.http.TestHttpResourceManager;

Expand Down Expand Up @@ -228,6 +228,7 @@ public Object createTestInstance(TestInstanceFactoryContext factoryContext, Exte
try {
Object instance = factoryContext.getTestClass().newInstance();
TestHttpResourceManager.inject(instance);
TestInjectionManager.inject(instance);
return instance;
} catch (InstantiationException | IllegalAccessException e) {
throw new TestInstantiationException("Failed to create test instance", e);
Expand Down

0 comments on commit 8680597

Please sign in to comment.