Skip to content

Commit

Permalink
Merge pull request #1804 from geoand/#1749
Browse files Browse the repository at this point in the history
Allow beans to be injected in tests even if they aren't used in the application
  • Loading branch information
geoand authored Apr 2, 2019
2 parents f222e12 + e278c77 commit be21697
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.deployment.builditem;

import org.jboss.builder.item.MultiBuildItem;

/**
* This is an optional build item that allows us to track annotations that will define test classes
* It is only available during tests
*/
public final class TestAnnotationBuildItem extends MultiBuildItem {

private final String annotationClassName;

public TestAnnotationBuildItem(String annotationClassName) {
this.annotationClassName = annotationClassName;
}

public String getAnnotationClassName() {
return annotationClassName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.arc.deployment;

import java.util.List;

import org.jboss.jandex.DotName;

import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.TestAnnotationBuildItem;

public class TestsAsBeanDefiningAnnotationsProcessor {

@BuildStep
public void produce(List<TestAnnotationBuildItem> items, BuildProducer<BeanDefiningAnnotationBuildItem> producer) {
for (TestAnnotationBuildItem item : items) {
producer.produce(new BeanDefiningAnnotationBuildItem(DotName.createSimple(item.getAnnotationClassName())));
}
}

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

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class UnusedBean {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.example.test;

import static org.junit.jupiter.api.Assertions.assertNotNull;

import javax.inject.Inject;

import org.junit.jupiter.api.Test;

import io.quarkus.example.arc.UnusedBean;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
class BeanOnlyInTestCase {

@Inject
UnusedBean unusedBean;

@Test
void assertBeanIsInjected() {
assertNotNull(unusedBean);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@
import static io.quarkus.test.common.PathTestHelper.getTestClassesLocation;

import java.io.IOException;
import java.util.function.Consumer;

import org.jboss.builder.BuildChainBuilder;
import org.jboss.builder.BuildContext;
import org.jboss.builder.BuildStep;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;

import io.quarkus.deployment.builditem.TestAnnotationBuildItem;
import io.quarkus.runner.RuntimeRunner;
import io.quarkus.runtime.LaunchMode;
import io.quarkus.test.common.PropertyTestUtil;
Expand All @@ -50,6 +56,18 @@ protected void startQuarkus() {
.setClassLoader(getClass().getClassLoader())
.setTarget(getAppClassLocation(getTestClass()))
.setFrameworkClassesPath(getTestClassesLocation(getTestClass()))
.addChainCustomizer(new Consumer<BuildChainBuilder>() {
@Override
public void accept(BuildChainBuilder buildChainBuilder) {
buildChainBuilder.addBuildStep(new BuildStep() {
@Override
public void execute(BuildContext context) {
context.produce(new TestAnnotationBuildItem(RunWith.class.getName()));
}
}).produces(TestAnnotationBuildItem.class)
.build();
}
})
.build();
runtimeRunner.run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import io.quarkus.bootstrap.util.PropertyUtils;
import io.quarkus.deployment.ClassOutput;
import io.quarkus.deployment.QuarkusClassWriter;
import io.quarkus.deployment.builditem.TestAnnotationBuildItem;
import io.quarkus.deployment.builditem.TestClassPredicateBuildItem;
import io.quarkus.deployment.util.IoUtil;
import io.quarkus.runner.RuntimeRunner;
Expand Down Expand Up @@ -243,6 +244,18 @@ public boolean test(String className) {
.build();
}
})
.addChainCustomizer(new Consumer<BuildChainBuilder>() {
@Override
public void accept(BuildChainBuilder buildChainBuilder) {
buildChainBuilder.addBuildStep(new BuildStep() {
@Override
public void execute(BuildContext context) {
context.produce(new TestAnnotationBuildItem(QuarkusTest.class.getName()));
}
}).produces(TestAnnotationBuildItem.class)
.build();
}
})
.build();
runtimeRunner.run();

Expand Down

0 comments on commit be21697

Please sign in to comment.