> found = getDeclaredConstructors(clazz).stream()
+ .filter((cons) -> isAnnotated(cons, annotationType))
+ .findFirst();
+
+ if (found.isPresent() || clazz.getSuperclass() == null) {
+ return found;
+ }
+
+ return findFirstAnnotatedConstructor(clazz.getSuperclass(), annotationType);
+ }
+
+ private static Class> unwrapInstanceTypeParameter(Field field) {
+ Class> type = field.getType();
+ if (type.equals(Instance.class)) {
+ ParameterizedType parameterizedType = (ParameterizedType) field.getGenericType();
+ Type typeParameter = parameterizedType.getActualTypeArguments()[0];
+ if (typeParameter instanceof ParameterizedType) {
+ type = (Class>) ((ParameterizedType) typeParameter).getRawType();
+ } else {
+ type = (Class>) typeParameter;
+ }
+ }
+ return type;
+ }
+}
diff --git a/spock/src/main/java/org/jboss/weld/spock/impl/EagerExceptionRenderer.java b/spock/src/main/java/org/jboss/weld/spock/impl/EagerExceptionRenderer.java
new file mode 100644
index 00000000..75a90def
--- /dev/null
+++ b/spock/src/main/java/org/jboss/weld/spock/impl/EagerExceptionRenderer.java
@@ -0,0 +1,56 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.impl;
+
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+import org.spockframework.runtime.SpockAssertionError;
+import org.spockframework.runtime.extension.IGlobalExtension;
+import org.spockframework.runtime.model.FeatureInfo;
+import org.spockframework.runtime.model.SpecInfo;
+
+/**
+ * A global Spock extension that eagerly renders exceptions, otherwise {@code toString()} might be called
+ * on a CDI proxy when the container is shut down already and the call cannot be done.
+ *
+ * If in a power assertion output you see something like
+ * {@code Something$Proxy$_$$_WeldClientProxy@4303056f (renderer threw IllegalStateException)},
+ * this extension might miss some places where to eagerly render the exception, and you should
+ * open an issue with a reproducer.
+ *
+ * @author Björn Kautler
+ */
+public class EagerExceptionRenderer implements IGlobalExtension {
+ @Override
+ public void visitSpec(SpecInfo spec) {
+ Stream
+ .concat(
+ StreamSupport.stream(spec.getAllFixtureMethods().spliterator(), false),
+ spec.getAllFeatures().stream().map(FeatureInfo::getFeatureMethod))
+ .forEach(method ->
+ method.addInterceptor(invocation -> {
+ try {
+ invocation.proceed();
+ } catch (SpockAssertionError spockAssertionError) {
+ spockAssertionError.toString();
+ throw spockAssertionError;
+ }
+ }));
+ }
+}
diff --git a/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldAutoInterceptor.java b/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldAutoInterceptor.java
new file mode 100644
index 00000000..02816f91
--- /dev/null
+++ b/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldAutoInterceptor.java
@@ -0,0 +1,129 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.impl;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.jboss.weld.environment.se.Weld;
+import org.jboss.weld.spock.EnableWeld;
+import org.jboss.weld.spock.WeldInitiator;
+import org.jboss.weld.spock.WeldSetup;
+import org.jboss.weld.spock.WeldSpockEnricher;
+import org.jboss.weld.spock.auto.ActivateScopes;
+import org.jboss.weld.spock.auto.AddBeanClasses;
+import org.jboss.weld.spock.auto.AddEnabledDecorators;
+import org.jboss.weld.spock.auto.AddEnabledInterceptors;
+import org.jboss.weld.spock.auto.AddExtensions;
+import org.jboss.weld.spock.auto.AddPackages;
+import org.jboss.weld.spock.auto.EnableAlternativeStereotypes;
+import org.jboss.weld.spock.auto.EnableAlternatives;
+import org.jboss.weld.spock.auto.ExcludeBean;
+import org.jboss.weld.spock.auto.ExcludeBeanClasses;
+import org.spockframework.runtime.InvalidSpecException;
+import org.spockframework.runtime.extension.IMethodInvocation;
+import org.spockframework.runtime.model.FieldInfo;
+import org.spockframework.runtime.model.SpecInfo;
+import spock.lang.Specification;
+
+import static java.lang.String.format;
+import static java.util.stream.Collectors.joining;
+import static java.util.stream.Collectors.toList;
+import static org.jboss.weld.spock.impl.ClassScanning.scanForRequiredBeanClasses;
+
+/**
+ * An alternative to {@link EnableWeldManualInterceptor} allowing to fully leverage an annotation based configuration approach.
+ * When selected by {@link EnableWeld#automagic()}, this interceptor will attempt to resolve all beans used in the test class
+ * and automatically adds them to the Weld container while bootstrapping it.
+ *
+ *
There are quite some annotations which can be used on the test class and on any discovered bean to configure it further.
+ *
+ *
Having a {@link WeldSetup @WeldSetup} annotated field in the specification or a super specification results in an
+ * exception, because it would not be considered and thus is a sign that the manual interceptor should be used, or that
+ * the field is a left-over from switching and should be removed.
+ *
+ *
Furthermore, all discovered {@link WeldSpockEnricher}s are invoked after the annotations are processed.
+ *
+ * @author Björn Kautler
+ * @see ActivateScopes
+ * @see AddBeanClasses
+ * @see AddEnabledDecorators
+ * @see AddEnabledInterceptors
+ * @see AddExtensions
+ * @see AddPackages
+ * @see EnableAlternatives
+ * @see EnableAlternativeStereotypes
+ * @see ExcludeBean
+ * @see ExcludeBeanClasses
+ * @see EnableWeld
+ * @see WeldSpockEnricher
+ */
+class EnableWeldAutoInterceptor extends EnableWeldInterceptor {
+ private final boolean explicitParamInjection;
+
+ public EnableWeldAutoInterceptor(List weldSpockEnrichers, boolean explicitParamInjection) {
+ super(weldSpockEnrichers);
+ this.explicitParamInjection = explicitParamInjection;
+ }
+
+ @Override
+ protected WeldInitiator weldInit(IMethodInvocation invocation) {
+ Specification testInstance = (Specification) invocation.getInstance();
+
+ SpecInfo spec = invocation.getSpec();
+ List weldSetupFields = spec
+ .getAllFields()
+ .stream()
+ .filter(field -> field.isAnnotationPresent(WeldSetup.class))
+ .collect(toList());
+
+ if (weldSetupFields.size() > 0) {
+ throw new InvalidSpecException(weldSetupFields
+ .stream()
+ .map(f -> format("Field '%s' with type %s which is in %s", f.getName(), f.getType(), f.getParent().getDisplayName()))
+ .collect(joining("\n", "When using automagic mode, no @WeldSetup annotated field should be present! Fields found:\n", "")));
+ }
+
+ Weld weld = WeldInitiator.createWeld();
+ WeldInitiator.Builder builder = WeldInitiator.from(weld);
+
+ scanForRequiredBeanClasses(spec.getReflection(), weld, explicitParamInjection);
+
+ weld.addBeanClasses(spec.getReflection());
+ weld.addExtension(new TestInstanceInjectionExtension<>(testInstance));
+
+ spec
+ .getSpecsBottomToTop()
+ .stream()
+ .map(s -> s.getAnnotationsByType(ActivateScopes.class))
+ .flatMap(Arrays::stream)
+ .map(ActivateScopes::value)
+ .flatMap(Arrays::stream)
+ .forEach(builder::activate);
+
+ // Apply discovered enrichers
+ for (WeldSpockEnricher enricher : weldSpockEnrichers) {
+ String property = System.getProperty(enricher.getClass().getName());
+ if (property == null || Boolean.parseBoolean(property)) {
+ enricher.enrich((testInstance == invocation.getSharedInstance()) ? null : testInstance, weld, builder);
+ }
+ }
+
+ return builder.build();
+ }
+}
diff --git a/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldExtension.java b/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldExtension.java
new file mode 100644
index 00000000..26d8987b
--- /dev/null
+++ b/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldExtension.java
@@ -0,0 +1,236 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.impl;
+
+import java.util.List;
+import java.util.Optional;
+import java.util.ServiceLoader;
+import java.util.stream.Stream;
+
+import org.jboss.weld.spock.DisableWeld;
+import org.jboss.weld.spock.EnableWeld;
+import org.jboss.weld.spock.EnableWeld.Scope;
+import org.jboss.weld.spock.WeldConfiguration;
+import org.jboss.weld.spock.WeldSpockEnricher;
+import org.jboss.weld.util.collections.ImmutableList;
+import org.spockframework.runtime.InvalidSpecException;
+import org.spockframework.runtime.extension.IGlobalExtension;
+import org.spockframework.runtime.model.FeatureInfo;
+import org.spockframework.runtime.model.MethodInfo;
+import org.spockframework.runtime.model.SpecInfo;
+import spock.lang.Shared;
+
+import static org.jboss.weld.spock.EnableWeld.Scope.FEATURE;
+import static org.jboss.weld.spock.EnableWeld.Scope.ITERATION;
+import static org.jboss.weld.spock.EnableWeld.Scope.SPECIFICATION;
+
+/**
+ * A global Spock extension evaluating the {@link EnableWeld @EnableWeld}, and {@link DisableWeld @DisableWeld} annotations,
+ * and the Spock configuration file options.
+ *
+ * If a feature has an {@code @EnableWeld} or {@code @DisableWeld} annotation applied, this is what is effective.
+ * If Weld is disabled, no container will be started for the feature or the iterations and no non-{@link Shared @Shared}
+ * fields, or method parameters will be injected. However, if a for the specification a {@code SPECIFICATION} scoped
+ * Weld was booted due to annotation or configuration file options, this container will have injected instances into
+ * the {@code @Shared} fields already.
+ *
+ *
If a feature has neither of the two annotations applied, the configuration of the specification is inherited.
+ *
+ *
If a specification has an {@code @EnableWeld} or {@code @DisableWeld} annotation applied, this is effective for
+ * all features that do not have an own annotation. It will have the same effect as if the annotation is copied to all
+ * features that have none of the two annotations already, except if scope {@code SPECIFICATION} is selected, as this
+ * is only valid in a specification level annotation or the Spock configuration file.
+ *
+ *
If a specification has neither of the two annotations applied, the super specifications are searched in order
+ * and if an annotated one is found, its annotation is effective as if it were on the specification directly.
+ *
+ *
If also no super specification has any of the annotations, the settings from the Spock configuration file
+ * or the respective default settings are effective.
+ *
+ * @author Björn Kautler
+ * @see EnableWeld
+ * @see DisableWeld
+ * @see WeldConfiguration
+ */
+public class EnableWeldExtension implements IGlobalExtension {
+ private final WeldConfiguration weldConfiguration;
+ private volatile List weldSpockEnrichers;
+
+ public EnableWeldExtension(WeldConfiguration weldConfiguration) {
+ this.weldConfiguration = weldConfiguration;
+ }
+
+ @Override
+ public void start() {
+ ImmutableList.Builder enrichers = ImmutableList.builder();
+ ServiceLoader.load(WeldSpockEnricher.class).forEach(enrichers::add);
+ weldSpockEnrichers = enrichers.build();
+ }
+
+ @Override
+ public void visitSpec(SpecInfo spec) {
+ Optional optionalAnnotatedSpec = spec
+ .getSpecsBottomToTop()
+ .stream()
+ .filter(specInfo -> specInfo.isAnnotationPresent(EnableWeld.class) || specInfo.isAnnotationPresent(DisableWeld.class))
+ .findFirst();
+
+ boolean doEnableWeldForSpec;
+ boolean specAutomagic;
+ Scope specScope;
+ boolean specExplicitParamInjection;
+ if (optionalAnnotatedSpec.isPresent()) {
+ SpecInfo annotatedSpec = optionalAnnotatedSpec.get();
+ EnableWeld enableWeld = annotatedSpec.getAnnotation(EnableWeld.class);
+ boolean enableWeldForSpec = enableWeld != null;
+ boolean disableWeldForSpec = annotatedSpec.isAnnotationPresent(DisableWeld.class);
+
+ if (enableWeldForSpec && disableWeldForSpec) {
+ throw new InvalidSpecException("@EnableWeld and @DisableWeld must not be used on the same spec: " + annotatedSpec.getDisplayName());
+ }
+
+ if (enableWeldForSpec) {
+ doEnableWeldForSpec = true;
+ specAutomagic = enableWeld.automagic();
+ specScope = enableWeld.scope();
+ specExplicitParamInjection = enableWeld.explicitParamInjection();
+ } else {
+ doEnableWeldForSpec = false;
+ specAutomagic = false;
+ specScope = null;
+ specExplicitParamInjection = false;
+ }
+ } else {
+ doEnableWeldForSpec = weldConfiguration.enabled;
+ specAutomagic = weldConfiguration.automagic;
+ specScope = weldConfiguration.scope == null ? ITERATION : weldConfiguration.scope;
+ specExplicitParamInjection = weldConfiguration.explicitParamInjection;
+ }
+
+ // boot Weld around specification and inject shared fields
+ EnableWeldInterceptor enableWeldInterceptorForSpec;
+ if (doEnableWeldForSpec && (specScope == SPECIFICATION)) {
+ enableWeldInterceptorForSpec = specAutomagic
+ ? new EnableWeldAutoInterceptor(weldSpockEnrichers, specExplicitParamInjection)
+ : new EnableWeldManualInterceptor(weldSpockEnrichers);
+ spec.addInterceptor(enableWeldInterceptorForSpec);
+
+ // inject parameters for specification fixture methods
+ Stream
+ .concat(
+ spec.getSetupSpecMethods().stream(),
+ spec.getCleanupSpecMethods().stream())
+ .forEach(method -> attachParameterInjector(method, enableWeldInterceptorForSpec, specExplicitParamInjection));
+ } else {
+ enableWeldInterceptorForSpec = null;
+ }
+
+ spec
+ .getAllFeatures()
+ .forEach(feature -> visitFeature(feature, doEnableWeldForSpec, specAutomagic, specScope,
+ specExplicitParamInjection, enableWeldInterceptorForSpec));
+ }
+
+ private void visitFeature(FeatureInfo feature, boolean doEnableWeldForSpec, boolean specAutomagic, Scope specScope,
+ boolean specExplicitParamInjection, EnableWeldInterceptor enableWeldInterceptorForSpec) {
+ MethodInfo featureMethod = feature.getFeatureMethod();
+ EnableWeld enableWeld = featureMethod.getAnnotation(EnableWeld.class);
+ boolean enableWeldForFeature = enableWeld != null;
+ boolean disableWeldForFeature = featureMethod.isAnnotationPresent(DisableWeld.class);
+
+ if (enableWeldForFeature && disableWeldForFeature) {
+ throw new InvalidSpecException("@EnableWeld and @DisableWeld must not be used on the same feature: " + feature.getDisplayName());
+ }
+
+ boolean doEnableWeldForFeature;
+ boolean featureAutomagic;
+ Scope featureScope;
+ boolean featureExplicitParamInjection;
+ if (enableWeldForFeature) {
+ doEnableWeldForFeature = true;
+ featureAutomagic = enableWeld.automagic();
+ featureScope = enableWeld.scope();
+ if (featureScope.compareTo(FEATURE) > 0) {
+ throw new InvalidSpecException("@EnableWeld on feature cannot have broader scope than FEATURE on feature: " + feature.getDisplayName());
+ }
+ featureExplicitParamInjection = enableWeld.explicitParamInjection();
+ } else if (disableWeldForFeature) {
+ doEnableWeldForFeature = false;
+ featureAutomagic = false;
+ featureScope = null;
+ featureExplicitParamInjection = false;
+ } else {
+ doEnableWeldForFeature = doEnableWeldForSpec;
+ featureAutomagic = specAutomagic;
+ featureScope = specScope;
+ featureExplicitParamInjection = specExplicitParamInjection;
+ }
+
+ if (doEnableWeldForFeature) {
+ EnableWeldInterceptor enableWeldInterceptorForFeature;
+
+ // boot Weld around feature or iteration and inject shared fields
+ switch (featureScope) {
+ case SPECIFICATION:
+ enableWeldInterceptorForFeature = enableWeldInterceptorForSpec;
+ enableWeldInterceptorForFeature.handleFeature(feature);
+ break;
+
+ case FEATURE:
+ enableWeldInterceptorForFeature = featureAutomagic
+ ? new EnableWeldAutoInterceptor(weldSpockEnrichers, featureExplicitParamInjection)
+ : new EnableWeldManualInterceptor(weldSpockEnrichers);
+ enableWeldInterceptorForFeature.handleFeature(feature);
+ feature.addInterceptor(enableWeldInterceptorForFeature);
+ break;
+
+ case ITERATION:
+ enableWeldInterceptorForFeature = featureAutomagic
+ ? new EnableWeldAutoInterceptor(weldSpockEnrichers, featureExplicitParamInjection)
+ : new EnableWeldManualInterceptor(weldSpockEnrichers);
+ enableWeldInterceptorForFeature.handleFeature(feature);
+ feature.addIterationInterceptor(enableWeldInterceptorForFeature);
+ break;
+
+ default:
+ throw new AssertionError();
+ }
+
+ // inject non-shared fields
+ feature.addIterationInterceptor(enableWeldInterceptorForFeature.getTestInstanceInjector());
+
+ // inject parameters for iteration methods
+ Stream.Builder streamBuilder = Stream.builder();
+ feature.getSpec().getSetupMethods().forEach(streamBuilder);
+ streamBuilder.add(featureMethod);
+ feature.getSpec().getCleanupMethods().forEach(streamBuilder);
+ streamBuilder
+ .build()
+ .forEach(method -> attachParameterInjector(method, enableWeldInterceptorForFeature, featureExplicitParamInjection));
+ }
+ }
+
+ private void attachParameterInjector(MethodInfo method, EnableWeldInterceptor enableWeldInterceptor, boolean explicitParamInjection) {
+ int amountOfParameters = method.getReflection().getParameters().length;
+ int amountOfDataVariables = method.getFeature() == null ? 0 : method.getFeature().getDataVariables().size();
+ // only attach if there could be injectable arguments
+ if (amountOfParameters > amountOfDataVariables) {
+ method.addInterceptor(enableWeldInterceptor.getParameterInjector(explicitParamInjection));
+ }
+ }
+}
diff --git a/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldInterceptor.java b/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldInterceptor.java
new file mode 100644
index 00000000..3b343bda
--- /dev/null
+++ b/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldInterceptor.java
@@ -0,0 +1,162 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.impl;
+
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Parameter;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.WeakHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import jakarta.enterprise.inject.spi.BeanManager;
+import org.jboss.weld.inject.WeldInstance;
+import org.jboss.weld.spock.WeldInitiator;
+import org.jboss.weld.spock.WeldSpockEnricher;
+import org.spockframework.runtime.extension.IMethodInterceptor;
+import org.spockframework.runtime.extension.IMethodInvocation;
+import org.spockframework.runtime.model.FeatureInfo;
+
+import static java.util.Collections.singleton;
+import static java.util.Collections.synchronizedMap;
+import static java.util.stream.Collectors.toList;
+import static org.spockframework.runtime.model.MethodInfo.MISSING_ARGUMENT;
+
+/**
+ * A Spock interceptor, that serves as base for the manual and automatic interceptors and has the common logic.
+ * It first calls the custom logic of the subclass, then adds the shared instance as injection target, initializes
+ * the Weld container and after invocation proceeded, shuts down the container. It also provides methods to get a
+ * test instance injector and a parameter injector as these need to be attached to varying extension points,
+ * depending on the annotation configuration.
+ *
+ * @author Björn Kautler
+ */
+abstract class EnableWeldInterceptor implements IMethodInterceptor {
+ protected final List weldSpockEnrichers;
+ private final Map weldInitiators = synchronizedMap(new WeakHashMap<>());
+ private final List handledFeatures = new CopyOnWriteArrayList<>();
+
+ public EnableWeldInterceptor(List weldSpockEnrichers) {
+ this.weldSpockEnrichers = weldSpockEnrichers;
+ }
+
+ protected abstract WeldInitiator weldInit(IMethodInvocation invocation);
+
+ @Override
+ public void intercept(IMethodInvocation invocation) throws Throwable {
+ WeldInitiator weldInitiator = weldInit(invocation);
+ weldInitiator.addObjectToInjectInto(invocation.getSharedInstance());
+ weldInitiator.initWeld(invocation.getInstance());
+ try {
+ Object id = (invocation.getIteration() == null) ? null : invocation.getInstance();
+ weldInitiators.put(id, weldInitiator);
+ invocation.proceed();
+ } finally {
+ weldInitiator.shutdownWeld();
+ }
+ }
+
+ public IMethodInterceptor getTestInstanceInjector() {
+ return invocation -> {
+ Object testInstance = invocation.getInstance();
+
+ WeldInitiator weldInitiator = weldInitiators.get(null);
+ if (weldInitiator == null) {
+ weldInitiator = weldInitiators.get(testInstance);
+ }
+
+ try (AutoCloseable contextReleaser = weldInitiator.injectNonContextual(testInstance)) {
+ invocation.proceed();
+ }
+ };
+ }
+
+ public void handleFeature(FeatureInfo feature) {
+ handledFeatures.add(feature);
+ }
+
+ public IMethodInterceptor getParameterInjector(boolean explicitParamInjection) {
+ return invocation -> {
+ // this is necessary so that a specification scoped interceptor
+ // does not inject into a feature or iteration scoped fixture method call
+ // or that a feature scoped interceptor of a data-driven feature
+ // does not inject into another features' fixture method calls
+ if ((invocation.getFeature() != null) && !handledFeatures.contains(invocation.getFeature())) {
+ invocation.proceed();
+ return;
+ }
+
+ // only continue if there still are missing arguments
+ if (Arrays.stream(invocation.getArguments())
+ .noneMatch(argument -> argument == MISSING_ARGUMENT)) {
+ invocation.proceed();
+ return;
+ }
+
+ Object testInstance = invocation.getInstance();
+
+ // get the sole weld initiator of this interceptor
+ // in case it is around a specification or parameterized feature
+ WeldInitiator weldInitiator = weldInitiators.get(null);
+ // or get the weld initiator for the current iteration
+ if (weldInitiator == null) {
+ weldInitiator = weldInitiators.get(testInstance);
+ }
+ // the fixture method interceptors for all features are triggered for each iteration
+ // so if there are multiple features not all have a matching initiator of course
+ if (weldInitiator == null) {
+ invocation.proceed();
+ return;
+ }
+
+ BeanManager beanManager = weldInitiator.getBeanManager();
+ Parameter[] parameters = invocation.getMethod().getReflection().getParameters();
+ Object[] arguments = invocation.getArguments();
+ for (int i = 0; i < arguments.length; i++) {
+ Object argument = arguments[i];
+ if (argument != MISSING_ARGUMENT) {
+ continue;
+ }
+
+ Parameter parameter = parameters[i];
+ List qualifiers = Arrays
+ .stream(parameter.getAnnotations())
+ .filter(annotation -> beanManager.isQualifier(annotation.annotationType()))
+ .collect(toList());
+
+ if (explicitParamInjection) {
+ if (qualifiers.isEmpty()) {
+ continue;
+ }
+ arguments[i] = weldInitiator
+ .select(parameter.getType(), qualifiers.toArray(new Annotation[0]))
+ .get();
+ } else {
+ WeldInstance> candidates = weldInitiator
+ .select(parameter.getType(), qualifiers.toArray(new Annotation[0]));
+ if (candidates.isResolvable()) {
+ arguments[i] = candidates.get();
+ }
+ }
+ }
+
+ invocation.proceed();
+ };
+ }
+}
diff --git a/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldManualInterceptor.java b/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldManualInterceptor.java
new file mode 100644
index 00000000..e961a39c
--- /dev/null
+++ b/spock/src/main/java/org/jboss/weld/spock/impl/EnableWeldManualInterceptor.java
@@ -0,0 +1,114 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.impl;
+
+import java.util.List;
+
+import org.jboss.weld.environment.se.Weld;
+import org.jboss.weld.spock.EnableWeld;
+import org.jboss.weld.spock.WeldInitiator;
+import org.jboss.weld.spock.WeldSetup;
+import org.jboss.weld.spock.WeldSpockEnricher;
+import org.spockframework.runtime.InvalidSpecException;
+import org.spockframework.runtime.extension.IMethodInvocation;
+import org.spockframework.runtime.model.FieldInfo;
+import spock.lang.Shared;
+import spock.lang.Specification;
+
+import static java.lang.String.format;
+import static java.util.stream.Collectors.joining;
+import static java.util.stream.Collectors.toList;
+
+/**
+ * A Spock interceptor that is used for manual configuration of the booted Weld container.
+ *
+ * If the interceptor is for scope {@code SPECIFICATION}, or for a data-driven feature with scope {@code FEATURE},
+ * {@link Shared @Shared} fields of the specification and its super specifications are searched for exactly one
+ * field that is annotated with {@link WeldSetup @WeldSetup}.
+ *
+ *
If multiple such fields are found, an exception is thrown.
+ *
+ *
If exactly one is found, and its value is not of type {@link WeldInitiator}, an exception is thrown.
+ *
+ *
Otherwise, as the type is correct, it is used as-is to initialize the Weld container.
+ *
+ *
If no field with that annotation is found, a new Weld initiator is created and the package of the test
+ * class is added as beans to the container. Then all discovered {@link WeldSpockEnricher}s are applied to that instance
+ * and the result is used to initialize the Weld container.
+ *
+ * @author Björn Kautler
+ * @see EnableWeld
+ * @see WeldSpockEnricher
+ */
+class EnableWeldManualInterceptor extends EnableWeldInterceptor {
+ public EnableWeldManualInterceptor(List weldSpockEnrichers) {
+ super(weldSpockEnrichers);
+ }
+
+ @Override
+ protected WeldInitiator weldInit(IMethodInvocation invocation) {
+ Specification spec = (Specification) invocation.getInstance();
+ boolean shared = spec == invocation.getSharedInstance();
+
+ List weldSetupFields = invocation
+ .getSpec()
+ .getAllFields()
+ .stream()
+ .filter(field -> (field.isShared() || field.isStatic()) == shared)
+ .filter(field -> field.isAnnotationPresent(WeldSetup.class))
+ .collect(toList());
+
+ switch (weldSetupFields.size()) {
+ case 0:
+ Weld weld = WeldInitiator.createWeld();
+ WeldInitiator.Builder builder = WeldInitiator.from(weld);
+
+ weld.addPackage(false, invocation.getSpec().getReflection());
+
+ // Apply discovered enrichers
+ for (WeldSpockEnricher enricher : weldSpockEnrichers) {
+ String property = System.getProperty(enricher.getClass().getName());
+ if (property == null || Boolean.parseBoolean(property)) {
+ enricher.enrich(shared ? null : spec, weld, builder);
+ }
+ }
+
+ return builder.build();
+
+ case 1:
+ FieldInfo weldSetupField = weldSetupFields.get(0);
+ Object weldSetupCandidate = weldSetupField.readValue(spec);
+ if (!(weldSetupCandidate instanceof WeldInitiator)) {
+ throw new InvalidSpecException(format(
+ "@WeldSetup annotation should only be used on a field with a "
+ + "WeldInitiator value but was found on field %s with a "
+ + "%s value which is declared in %s",
+ weldSetupField.getName(),
+ ((weldSetupCandidate == null) ? "null" : weldSetupCandidate.getClass()),
+ weldSetupField.getParent().getDisplayName()));
+ }
+ return (WeldInitiator) weldSetupCandidate;
+
+ default:
+ throw new InvalidSpecException(weldSetupFields
+ .stream()
+ .map(f -> format("Field '%s' with type %s which is in %s", f.getName(), f.getType(), f.getParent().getDisplayName()))
+ .collect(joining("\n", "Multiple @WeldSetup annotated fields found, only one is allowed! Fields found:\n", "")));
+ }
+ }
+}
diff --git a/spock/src/main/java/org/jboss/weld/spock/impl/ExcludedBeansExtension.java b/spock/src/main/java/org/jboss/weld/spock/impl/ExcludedBeansExtension.java
new file mode 100644
index 00000000..c304f5a4
--- /dev/null
+++ b/spock/src/main/java/org/jboss/weld/spock/impl/ExcludedBeansExtension.java
@@ -0,0 +1,58 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.impl;
+
+import java.lang.reflect.Type;
+import java.util.Set;
+
+import jakarta.enterprise.context.NormalScope;
+import jakarta.enterprise.event.Observes;
+import jakarta.enterprise.inject.spi.Extension;
+import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
+import jakarta.enterprise.inject.spi.WithAnnotations;
+import jakarta.inject.Scope;
+
+/**
+ * Extension class that ensures selected classes are excluded as beans.
+ *
+ * @author Björn Kautler
+ */
+class ExcludedBeansExtension implements Extension {
+ private final Set excludedBeanTypes;
+ private final Set> excludedBeanClasses;
+
+ ExcludedBeansExtension(Set excludedBeanTypes, Set> excludedBeanClasses) {
+ this.excludedBeanTypes = excludedBeanTypes;
+ this.excludedBeanClasses = excludedBeanClasses;
+ }
+
+ void excludeBeans(@Observes @WithAnnotations({ Scope.class, NormalScope.class }) ProcessAnnotatedType pat) {
+ if (excludedBeanClasses.contains(pat.getAnnotatedType().getJavaClass())) {
+ pat.veto();
+ return;
+ }
+
+ Set typeClosure = pat.getAnnotatedType().getTypeClosure();
+ for (Type excludedBeanType : excludedBeanTypes) {
+ if (typeClosure.contains(excludedBeanType)) {
+ pat.veto();
+ return;
+ }
+ }
+ }
+}
diff --git a/spock/src/main/java/org/jboss/weld/spock/impl/TestInstanceInjectionExtension.java b/spock/src/main/java/org/jboss/weld/spock/impl/TestInstanceInjectionExtension.java
new file mode 100644
index 00000000..3d4e3b3d
--- /dev/null
+++ b/spock/src/main/java/org/jboss/weld/spock/impl/TestInstanceInjectionExtension.java
@@ -0,0 +1,76 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.impl;
+
+import jakarta.enterprise.context.spi.CreationalContext;
+import jakarta.enterprise.event.Observes;
+import jakarta.enterprise.inject.spi.Extension;
+import jakarta.enterprise.inject.spi.InjectionTarget;
+import jakarta.enterprise.inject.spi.ProcessAnnotatedType;
+import jakarta.enterprise.inject.spi.ProcessInjectionTarget;
+import jakarta.enterprise.util.AnnotationLiteral;
+import jakarta.inject.Singleton;
+import org.jboss.weld.injection.ForwardingInjectionTarget;
+
+/**
+ * Extension that makes a test instance appear like a regular bean even though instantiated by JUnit.
+ *
+ * @author Björn Kautler
+ */
+class TestInstanceInjectionExtension implements Extension {
+ private static final AnnotationLiteral SINGLETON_LITERAL = new AnnotationLiteral() {
+ };
+
+ private final Class> testClass;
+ private final T testInstance;
+
+ TestInstanceInjectionExtension(T testInstance) {
+ this.testClass = testInstance.getClass();
+ this.testInstance = testInstance;
+ }
+
+ void rewriteTestClassScope(@Observes ProcessAnnotatedType pat) {
+ if (pat.getAnnotatedType().getJavaClass().equals(testClass)) {
+ pat.configureAnnotatedType().add(SINGLETON_LITERAL);
+ }
+ }
+
+ private class TestInstanceInjectionTarget extends ForwardingInjectionTarget {
+ private final InjectionTarget injectionTarget;
+
+ TestInstanceInjectionTarget(InjectionTarget injectionTarget) {
+ this.injectionTarget = injectionTarget;
+ }
+
+ @Override
+ protected InjectionTarget delegate() {
+ return injectionTarget;
+ }
+
+ @Override
+ public T produce(CreationalContext creationalContext) {
+ return testInstance;
+ }
+ }
+
+ void rewriteTestInstanceInjectionTarget(@Observes ProcessInjectionTarget pit) {
+ if (pit.getAnnotatedType().getJavaClass().equals(testClass)) {
+ pit.setInjectionTarget(new TestInstanceInjectionTarget(pit.getInjectionTarget()));
+ }
+ }
+}
diff --git a/spock/src/main/resources/META-INF/services/org.spockframework.runtime.extension.IGlobalExtension b/spock/src/main/resources/META-INF/services/org.spockframework.runtime.extension.IGlobalExtension
new file mode 100644
index 00000000..ea65f914
--- /dev/null
+++ b/spock/src/main/resources/META-INF/services/org.spockframework.runtime.extension.IGlobalExtension
@@ -0,0 +1,2 @@
+org.jboss.weld.spock.impl.EnableWeldExtension
+org.jboss.weld.spock.impl.EagerExceptionRenderer
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/alternative/AlternativeAsSoleBeanInSyntheticArchiveTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/alternative/AlternativeAsSoleBeanInSyntheticArchiveTest.groovy
new file mode 100644
index 00000000..adf806d2
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/alternative/AlternativeAsSoleBeanInSyntheticArchiveTest.groovy
@@ -0,0 +1,62 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.alternative
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+import org.jboss.weld.junit.MockBean
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Issue
+import spock.lang.Specification
+
+import static org.jboss.weld.spock.WeldInitiator.createWeld
+
+/**
+ * This mimics problems in issue 64, but without the need for discovery
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld
+class AlternativeAsSoleBeanInSyntheticArchiveTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(createWeld())
+ .addBeans(createSelectedAlternativeBean())
+ .build()
+
+ static createSelectedAlternativeBean() {
+ return MockBean
+ .builder()
+ .types(Fish)
+ .scope(ApplicationScoped)
+ .selectedAlternative(Fish)
+ .creating(new Fish(200))
+ .build()
+ }
+
+ @Inject
+ Fish fish
+
+ @Issue('https://github.com/weld/weld-junit/issues/64')
+ def 'synthetic archive with only enabled alternative should still work'() {
+ expect:
+ fish.numberOfLegs == 200
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/alternative/Fish.groovy b/spock/src/test/groovy/org/jboss/weld/spock/alternative/Fish.groovy
new file mode 100644
index 00000000..59c7290c
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/alternative/Fish.groovy
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.alternative
+
+/**
+ * @author Björn Kautler
+ */
+class Fish {
+ private int legs
+
+ Fish(int numberOfLegs) {
+ legs = numberOfLegs
+ }
+
+ int getNumberOfLegs() {
+ return legs
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ActivateScopesInheritanceTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ActivateScopesInheritanceTest.groovy
new file mode 100644
index 00000000..8c30610a
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ActivateScopesInheritanceTest.groovy
@@ -0,0 +1,75 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.ContextNotActiveException
+import jakarta.enterprise.context.ConversationScoped
+import jakarta.enterprise.context.RequestScoped
+import jakarta.enterprise.context.SessionScoped
+import jakarta.enterprise.inject.Produces
+import jakarta.enterprise.inject.spi.BeanManager
+import jakarta.inject.Named
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.beans.Engine
+import org.jboss.weld.spock.auto.beans.V6
+import org.jboss.weld.spock.auto.beans.V8
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+class ActivateScopesInheritanceTest extends BaseActivateScopesInheritanceTest {
+ @Produces
+ @SessionScoped
+ @Named("special")
+ V8 sessionEngine = new V8()
+
+ @Produces
+ @ConversationScoped
+ // V6 is annotated with @ApplicationScoped, this tells the container to use producer instead
+ @ExcludeBean
+ V6 convoEngine = new V6()
+
+ def '@ActivateScopes should activate the specified scopes'(BeanManager beanManager) {
+ expect:
+ beanManager.getContext(RequestScoped).active
+ beanManager.getContext(SessionScoped).active
+ }
+
+ def 'Engine should be resolved to @SessionScoped V8'(@Named("special") Engine engine) {
+ expect:
+ engine.throttle == 0
+ }
+
+ def 'non-activated scopes should fail'(V6 engine) {
+ when:
+ engine.throttle
+
+ then:
+ thrown(ContextNotActiveException)
+ }
+}
+
+/**
+ * Base class for {@link ActivateScopesInheritanceTest} to test if annotations are inherited.
+ */
+@EnableWeld(automagic = true)
+@ActivateScopes([SessionScoped, RequestScoped])
+class BaseActivateScopesInheritanceTest extends Specification {
+ // empty, just a base class to let annotations be inherited
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ActivateScopesTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ActivateScopesTest.groovy
new file mode 100644
index 00000000..345e20e6
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ActivateScopesTest.groovy
@@ -0,0 +1,68 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.ContextNotActiveException
+import jakarta.enterprise.context.ConversationScoped
+import jakarta.enterprise.context.RequestScoped
+import jakarta.enterprise.context.SessionScoped
+import jakarta.enterprise.inject.Produces
+import jakarta.enterprise.inject.spi.BeanManager
+import jakarta.inject.Named
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.beans.Engine
+import org.jboss.weld.spock.auto.beans.V6
+import org.jboss.weld.spock.auto.beans.V8
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@ActivateScopes([SessionScoped, RequestScoped])
+class ActivateScopesTest extends Specification {
+ @Produces
+ @SessionScoped
+ @Named('special')
+ V8 sessionEngine = new V8()
+
+ @Produces
+ @ConversationScoped
+ // V6 is annotated with @ApplicationScoped, this tells the container to use this producer instead
+ @ExcludeBean
+ V6 convoEngine = new V6()
+
+ def '@ActivateScopes should activate the specified scopes'(BeanManager beanManager) {
+ expect:
+ beanManager.getContext(RequestScoped).active
+ beanManager.getContext(SessionScoped).active
+ }
+
+ def 'Engine should be resolved to @SessionScoped V8'(@Named("special") Engine engine) {
+ expect:
+ engine.throttle == 0
+ }
+
+ def 'non-activated scopes should fail'(V6 engine) {
+ when:
+ engine.throttle
+
+ then:
+ thrown(ContextNotActiveException)
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/AddBeanClassesTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddBeanClassesTest.groovy
new file mode 100644
index 00000000..8fb4ee3f
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddBeanClassesTest.groovy
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.beans.Engine
+import org.jboss.weld.spock.auto.beans.V8
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(V8)
+class AddBeanClassesTest extends Specification {
+ @Inject
+ private Engine engine
+
+ def '@AddBeanClasses should pull in V8 to fulfill the injected Engine interface'() {
+ expect:
+ engine != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/AddDecoratorTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddDecoratorTest.groovy
new file mode 100644
index 00000000..b415984f
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddDecoratorTest.groovy
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.interceptorAndDecorator.DecoratedBean
+import org.jboss.weld.spock.auto.interceptorAndDecorator.TestDecorator
+import spock.lang.Specification
+
+/**
+ * Test that you can add decorator class and enable it (no need for priority) via annotation.
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddEnabledDecorators(TestDecorator)
+class AddDecoratorTest extends Specification {
+ @Inject
+ DecoratedBean bean
+
+ def 'bean should be decorated'() {
+ expect:
+ bean != null
+ bean.ping() == TestDecorator.toString() + DecoratedBean.toString()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/AddExtensionsTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddExtensionsTest.groovy
new file mode 100644
index 00000000..0b2c5394
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddExtensionsTest.groovy
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.extension.AddedExtension
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddExtensions(AddedExtension)
+class AddExtensionsTest extends Specification {
+ def '@AddExtensions should add the specified extensions'() {
+ expect:
+ AddedExtension.enabled
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/AddInterceptorTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddInterceptorTest.groovy
new file mode 100644
index 00000000..4d266638
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddInterceptorTest.groovy
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.interceptorAndDecorator.InterceptedBean
+import org.jboss.weld.spock.auto.interceptorAndDecorator.TestInterceptor
+import spock.lang.Specification
+
+/**
+ * Test that you can add interceptor class and enable it (no need for priority) via annotation.
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddEnabledInterceptors(TestInterceptor)
+class AddInterceptorTest extends Specification {
+ @Inject
+ InterceptedBean bean
+
+ def 'bean should be intercepted'() {
+ expect:
+ bean.ping() == TestInterceptor.toString() + InterceptedBean.toString()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/AddPackagesTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddPackagesTest.groovy
new file mode 100644
index 00000000..8c6954d8
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/AddPackagesTest.groovy
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.beans.Engine
+import org.jboss.weld.spock.auto.beans.V8
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddPackages(value = Engine, recursively = false)
+class AddPackagesTest extends Specification {
+ @Inject
+ private V8 engine
+
+ def '@AddPackages should pull in V8 to fulfill the injected Engine interface'() {
+ expect:
+ engine != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/AnnotationsInheritanceTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/AnnotationsInheritanceTest.groovy
new file mode 100644
index 00000000..047cee87
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/AnnotationsInheritanceTest.groovy
@@ -0,0 +1,106 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.beans.Engine
+import org.jboss.weld.spock.auto.beans.V8
+import org.jboss.weld.spock.auto.extension.AddedExtension
+import org.jboss.weld.spock.auto.interceptorAndDecorator.DecoratedBean
+import org.jboss.weld.spock.auto.interceptorAndDecorator.InterceptedBean
+import org.jboss.weld.spock.auto.interceptorAndDecorator.TestDecorator
+import org.jboss.weld.spock.auto.interceptorAndDecorator.TestInterceptor
+import spock.lang.Specification
+
+/**
+ * Tests the inheritance of "Weld-Spock" annotations from test parent classes.
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(V8)
+class BaseAddBeanClassesTest extends Specification {
+}
+
+class AnnotationsInheritanceAddBeanClassesTest extends BaseAddBeanClassesTest {
+ @Inject
+ private Engine engine
+
+ def '@AddBeanClasses should pull in V8 to fulfill the injected Engine interface'() {
+ expect:
+ engine != null
+ }
+}
+
+@EnableWeld(automagic = true)
+@AddEnabledDecorators(TestDecorator)
+class BaseAddDecoratorTest extends Specification {
+}
+
+class AnnotationsInheritanceAddDecoratorTest extends BaseAddDecoratorTest {
+ @Inject
+ DecoratedBean bean
+
+ def 'bean should be decorated'() {
+ expect:
+ bean.ping() == TestDecorator.toString() + DecoratedBean.toString()
+ }
+}
+
+@EnableWeld(automagic = true)
+@AddExtensions(AddedExtension)
+class BaseAddExtensionsTest extends Specification {
+}
+
+class AnnotationsInheritanceAddExtensionsTest extends BaseAddExtensionsTest {
+ def '@AddExtensions should add the specified extensions'() {
+ expect:
+ AddedExtension.enabled
+ }
+}
+
+@EnableWeld(automagic = true)
+@AddEnabledInterceptors(TestInterceptor)
+class BaseAddInterceptorTest extends Specification {
+}
+
+class AnnotationsInheritanceAddInterceptorTest extends BaseAddInterceptorTest {
+ @Inject
+ InterceptedBean bean
+
+ def 'bean should be intercepted'() {
+ expect:
+ bean.ping() == TestInterceptor.toString() + InterceptedBean.toString()
+ }
+}
+
+@EnableWeld(automagic = true)
+@AddPackages(value = Engine, recursively = false)
+class BaseAddPackagesTest {
+}
+
+class AnnotationsInheritanceAddPackagesTest extends BaseAddPackagesTest {
+ @Inject
+ private V8 engine
+
+ def '@AddPackages should pull in V8 (without bean defining annotation) to fulfill the injected Engine interface'() {
+ expect:
+ engine != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/AutoConfigWithWeldSetupTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/AutoConfigWithWeldSetupTest.groovy
new file mode 100644
index 00000000..eaa28d8a
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/AutoConfigWithWeldSetupTest.groovy
@@ -0,0 +1,60 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import org.jboss.weld.spock.util.EmbeddedSpecRunnerWrapper
+import org.spockframework.runtime.InvalidSpecException
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+class AutoConfigWithWeldSetupTest extends Specification {
+ def '@WeldSetup should not be compatible with automagic mode'() {
+ given:
+ def runner = new EmbeddedSpecRunnerWrapper()
+ runner.addClassImport(EnableWeld)
+ runner.addClassImport(WeldSetup)
+ runner.addClassImport(WeldInitiator)
+ runner.addClassMemberImport(WeldInitiator)
+
+ when:
+ runner.runWithImports '''
+ @EnableWeld(automagic = true)
+ class Foo extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.of(createWeld())
+
+ def bar() {
+ expect:
+ 'IllegalStateException expected' == 'before feature method is entered'
+ }
+ }
+ '''
+
+ then:
+ InvalidSpecException ise = thrown()
+ ise.message == '''
+ When using automagic mode, no @WeldSetup annotated field should be present! Fields found:
+ Field 'weld' with type class java.lang.Object which is in Foo
+ '''.stripIndent(true).trim()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/BasicAutoConfigTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/BasicAutoConfigTest.groovy
new file mode 100644
index 00000000..9975d52c
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/BasicAutoConfigTest.groovy
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.Foo
+import org.jboss.weld.spock.explicitInjection.Bar
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+class BasicAutoConfigTest extends Specification {
+ @Inject
+ private Foo foo
+
+ @Inject
+ private Bar bar
+
+ def 'the injected Foo and Bar should be automatically included in container with no configuration'() {
+ expect:
+ bar != null
+ foo.bar == 'baz'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanClassesDepsTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanClassesDepsTest.groovy
new file mode 100644
index 00000000..9e2230b2
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanClassesDepsTest.groovy
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.inject.Produces
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.unsatisfied.FooDeps
+import org.jboss.weld.spock.basic.unsatisfied.SomeFooDeps
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(SomeFooDeps)
+@ExcludeBeanClasses(FooDeps)
+class ExcludeBeanClassesDepsTest extends Specification {
+ /**
+ * FooDeps injects the Baz bean which has an unsatisfied dependency "bar-value". Excluding FooDeps should ensure
+ * that its specific dependencies are not included via scanning and therefore don't need to be provided for
+ * testing.
+ */
+
+ @Produces
+ FooDeps fakeFooDeps = new FooDeps()
+
+ def '@ExcludeBeanClasses should exclude any specific dependencies of the excluded classes'(FooDeps myFooDeps) {
+ expect:
+ myFooDeps != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanClassesTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanClassesTest.groovy
new file mode 100644
index 00000000..975fd0a8
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanClassesTest.groovy
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.inject.Produces
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.Foo
+import org.jboss.weld.spock.basic.SomeFoo
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(SomeFoo)
+@ExcludeBeanClasses(Foo)
+class ExcludeBeanClassesTest extends Specification {
+ @Produces
+ static Foo fakeFoo = new Foo('non-baz')
+
+ def '@ExcludeBeanClasses should exclude the specified classes'(Foo myFoo) {
+ expect:
+ myFoo.bar == 'non-baz'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanDepsTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanDepsTest.groovy
new file mode 100644
index 00000000..2a802cd8
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanDepsTest.groovy
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.inject.Produces
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.unsatisfied.FooDeps
+import org.jboss.weld.spock.basic.unsatisfied.SomeFooDeps
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(SomeFooDeps)
+class ExcludeBeanDepsTest extends Specification {
+ /**
+ * FooDeps injects the Baz bean which has an unsatisfied dependency "bar-value". Excluding FooDeps should ensure
+ * that its specific dependencies are not included via scanning and therefore don't need to be provided for
+ * testing.
+ */
+
+ @Produces
+ @ExcludeBean
+ FooDeps fakeFooDeps = new FooDeps()
+
+ def '@ExcludeBean should exclude any specific dependencies of the excluded class'(FooDeps myFooDeps) {
+ expect:
+ myFooDeps != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanHierarchyTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanHierarchyTest.groovy
new file mode 100644
index 00000000..285328c4
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanHierarchyTest.groovy
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.inject.Produces
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.Foo
+import org.jboss.weld.spock.basic.IFoo
+import org.jboss.weld.spock.basic.SomeIFoo
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(SomeIFoo)
+class ExcludeBeanHierarchyTest extends Specification {
+ @Produces
+ @ExcludeBean
+ IFoo fakeFoo = new Foo('non-baz')
+
+ def '@ExcludeBean should exclude all beans in implied hierarchy'(IFoo myIFoo) {
+ expect:
+ myIFoo.bar == 'non-baz'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanTest.groovy
new file mode 100644
index 00000000..bd63c73e
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExcludeBeanTest.groovy
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.inject.Produces
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.Foo
+import org.jboss.weld.spock.basic.SomeFoo
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(SomeFoo)
+class ExcludeBeanTest extends Specification {
+ @Produces
+ @ExcludeBean
+ Foo fakeFoo = new Foo('non-baz')
+
+ def '@ExcludeBean should exclude the implied bean class'(Foo myFoo) {
+ expect:
+ myFoo.bar == 'non-baz'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ExplicitParametersAutoConfigTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExplicitParametersAutoConfigTest.groovy
new file mode 100644
index 00000000..57bb3ce7
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ExplicitParametersAutoConfigTest.groovy
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.inject.Default
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.Foo
+import org.jboss.weld.spock.explicitInjection.Bar
+import org.jboss.weld.spock.explicitInjection.Custom
+import org.jboss.weld.spock.explicitInjection.CustomExtension
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true, explicitParamInjection = true)
+class ExplicitParametersAutoConfigTest extends Specification {
+ @Custom
+ def 'the parameter Foo should be automatically included in container and Bar should come from @Custom'(@Default Foo foo, Bar bar) {
+ expect:
+ bar.ping() == CustomExtension.simpleName
+ foo.bar == 'baz'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedInjectedTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedInjectedTest.groovy
new file mode 100644
index 00000000..6ed7bdcc
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedInjectedTest.groovy
@@ -0,0 +1,62 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.Dependent
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class InheritedInjectedTestFoo {
+}
+
+@EnableWeld(automagic = true)
+class InheritedInjectedTestBaseClass extends Specification {
+ @Inject
+ InheritedInjectedTestFoo foo
+}
+
+class InjectIntoInheritedFieldsFromTestBaseClassTest extends InheritedInjectedTestBaseClass {
+ def 'inherited fields of a super spec should be injected'() {
+ expect:
+ foo != null
+ }
+}
+
+@Dependent
+class SubClass extends InheritedInjectedTestBaseClass {
+}
+
+@EnableWeld(automagic = true)
+class InjectIntoInheritedFieldsFromBeanBaseClassTest extends Specification {
+ @Inject
+ SubClass inheritsInjected
+
+ /**
+ * Injection of {@link #inheritsInjected} requires {@link InheritedInjectedTestFoo}
+ * having been identified and added as a bean class to Weld.
+ */
+ def 'inherited field in bean should be injected'() {
+ expect:
+ inheritsInjected?.foo != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedProducerFieldTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedProducerFieldTest.groovy
new file mode 100644
index 00000000..8aeba0dc
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedProducerFieldTest.groovy
@@ -0,0 +1,52 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.Dependent
+import jakarta.enterprise.inject.Produces
+import jakarta.enterprise.inject.spi.BeanManager
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class InheritedProducerFieldTestFoo {
+}
+
+class InheritedProducerFieldTestBaseClass {
+ @Produces
+ InheritedProducerFieldTestFoo baseFooProducer = new InheritedProducerFieldTestFoo()
+}
+
+@Dependent
+class InheritedProducerFieldTestSubClass extends InheritedProducerFieldTestBaseClass {
+}
+
+@EnableWeld(automagic = true)
+class DontAddBeanClassesFromFieldsInheritedFromBeanBaseClassTest extends Specification {
+ @Inject
+ InheritedProducerFieldTestSubClass subClass
+
+ def 'inherited producer field type from bean base class should not be added to the container'(BeanManager beanManager) {
+ expect:
+ beanManager.getBeans(InheritedProducerFieldTestFoo).size() == 0
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedProducerMethodTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedProducerMethodTest.groovy
new file mode 100644
index 00000000..1e495126
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/InheritedProducerMethodTest.groovy
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.Dependent
+import jakarta.enterprise.inject.Produces
+import jakarta.enterprise.inject.spi.BeanManager
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class InheritedProducerMethodTestFoo {
+}
+
+class InheritedProducerMethodTestBaseClass {
+ @Produces
+ InheritedProducerMethodTestFoo baseFooProducer() {
+ new InheritedProducerMethodTestFoo()
+ }
+}
+
+@Dependent
+class InheritedProducerMethodTestSubClass extends InheritedProducerMethodTestBaseClass {
+}
+
+@EnableWeld(automagic = true)
+class DontAddBeanClassesFromMethodInheritedFromBeanBaseClassTest extends Specification {
+ @Inject
+ InheritedProducerMethodTestSubClass subClass
+
+ void 'inherited producer method return type from bean base class should not be added to the container'(BeanManager beanManager) {
+ expect:
+ beanManager.getBeans(InheritedProducerMethodTestFoo).size() == 0
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectInstanceTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectInstanceTest.groovy
new file mode 100644
index 00000000..a6cddb08
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectInstanceTest.groovy
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.Dependent
+import jakarta.enterprise.inject.Instance
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+class InjectInstanceTest extends Specification {
+ @Dependent
+ static class Foo {
+ }
+
+ @Inject
+ Instance fooInstance
+
+ def 'injecting Instance should work properly'() {
+ expect:
+ fooInstance.get() != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectParameterizedInstanceTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectParameterizedInstanceTest.groovy
new file mode 100644
index 00000000..4b83d8e7
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectParameterizedInstanceTest.groovy
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.Dependent
+import jakarta.enterprise.inject.Instance
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+class InjectParameterizedInstanceTest extends Specification {
+ @Dependent
+ static class Foo {
+ }
+
+ @Inject
+ Instance> fooInstance
+
+ def 'injecting Instance of parameterized type should work properly'() {
+ expect:
+ fooInstance.get() != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectParameterizedTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectParameterizedTest.groovy
new file mode 100644
index 00000000..0faa6ae9
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/InjectParameterizedTest.groovy
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.Dependent
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+class InjectParameterizedTest extends Specification {
+ @Dependent
+ static class Foo {
+ }
+
+ @Inject
+ Foo foo
+
+ def 'injecting parameterized type should work properly'() {
+ expect:
+ foo != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ParametersAutoConfigTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ParametersAutoConfigTest.groovy
new file mode 100644
index 00000000..b411e2e0
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ParametersAutoConfigTest.groovy
@@ -0,0 +1,36 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.Foo
+import org.jboss.weld.spock.explicitInjection.Bar
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+class ParametersAutoConfigTest extends Specification {
+ def 'the parameters Foo and Bar should be automatically included in container with no configuration'(Foo foo, Bar bar) {
+ expect:
+ bar != null
+ bar.ping() == null
+ foo?.bar == 'baz'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ProducerMethodParametersScanningTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ProducerMethodParametersScanningTest.groovy
new file mode 100644
index 00000000..2476a4eb
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ProducerMethodParametersScanningTest.groovy
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.Dependent
+import jakarta.enterprise.inject.Produces
+import jakarta.inject.Named
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.beans.Engine
+import org.jboss.weld.spock.auto.beans.V6
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+class ProducerMethodParametersScanningTest extends Specification {
+ @Produces
+ @Named('custom')
+ @Dependent
+ Engine getEngine(V6 v6) {
+ new Engine() {
+ @Override
+ int getThrottle() {
+ v6.throttle
+ }
+
+ @Override
+ void setThrottle(int value) {
+ v6.throttle = value
+ }
+ }
+ }
+
+ def 'parameters of producer methods should be automatically added to container'(@Named('custom') Engine engine) {
+ expect:
+ engine != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ProducesBeforeTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ProducesBeforeTest.groovy
new file mode 100644
index 00000000..a7608fde
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ProducesBeforeTest.groovy
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.context.Dependent
+import jakarta.enterprise.inject.Produces
+import jakarta.inject.Named
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+class ProducesBeforeTest extends Specification {
+ private Integer requiredValue
+
+ def setup() {
+ requiredValue = 10
+ }
+
+ @Produces
+ @Dependent
+ @Named("computed")
+ Integer provideComputedValue() {
+ return requiredValue * 10
+ }
+
+ def 'setup should run before any @Produces methods/fields are interrogated'(@Named("computed") Integer computed) {
+ expect:
+ computed != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ScannedClassesAreNotForcedBeansTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ScannedClassesAreNotForcedBeansTest.groovy
new file mode 100644
index 00000000..13c391da
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ScannedClassesAreNotForcedBeansTest.groovy
@@ -0,0 +1,51 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.inject.Produces
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.beans.unsatisfied.InjectedV8NoAnnotation
+import org.jboss.weld.spock.auto.beans.unsatisfied.V8NoAnnotation
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(InjectedV8NoAnnotation)
+class ScannedClassesAreNotForcedBeansTest extends Specification {
+ /**
+ * V8 is *not* a "bean" class, in that it has no bean defining annotation. To satisfy
+ * a dependency on it, a producer method or a reference in an @AddBeanClasses annotation
+ * is required.
+ *
+ * This test ensures that as V8 is discovered via class scanning it is not automatically
+ * added as a bean class. If it was added that way, the bean class and producer method would
+ * create an ambiguous injection case for V8.
+ *
+ * NOTE: This case only tests for classes found as non-parameters (e.g. injected fields)
+ */
+
+ @Produces
+ private V8NoAnnotation engine = new V8NoAnnotation()
+
+ def 'V8NoAnnotation should not be ambiguous for not being incorrectly identified as a bean class'(V8NoAnnotation engine) {
+ expect:
+ engine != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/ScannedParameterClassesAreNotForcedBeansTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/ScannedParameterClassesAreNotForcedBeansTest.groovy
new file mode 100644
index 00000000..80f1d15d
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/ScannedParameterClassesAreNotForcedBeansTest.groovy
@@ -0,0 +1,52 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto
+
+import jakarta.enterprise.inject.Produces
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.beans.unsatisfied.ConstructedV8NoAnnotation
+import org.jboss.weld.spock.auto.beans.unsatisfied.V8NoAnnotation
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses(ConstructedV8NoAnnotation)
+class ScannedParameterClassesAreNotForcedBeansTest extends Specification {
+ /**
+ * V8NoAnnotation is *not* a "bean" class, in that it has no bean defining annotation. To satisfy
+ * a dependency on it, a producer method or a reference in an @AddBeanClasses annotation
+ * is required.
+ *
+ * This test ensures that as V8NoAnnotation is discovered via class scanning it is not automatically
+ * added as a bean class. If it was added that way, the bean class and producer method would
+ * create an ambiguous injection case for V8.
+ *
+ * NOTE: This case only tests for classes found from parameters (e.g. constructor injection
+ * parameters)
+ */
+
+ @Produces
+ private V8NoAnnotation engine = new V8NoAnnotation()
+
+ def 'V8NoAnnotation should not be ambiguous for not being incorrectly identified as a bean class from parameter'(V8NoAnnotation engine) {
+ expect:
+ engine != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/EnableAlternativeStereotypeInheritanceTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/EnableAlternativeStereotypeInheritanceTest.groovy
new file mode 100644
index 00000000..eb8b7f39
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/EnableAlternativeStereotypeInheritanceTest.groovy
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternativeStereotype
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.AddBeanClasses
+import org.jboss.weld.spock.auto.EnableAlternativeStereotypes
+import spock.lang.Specification
+
+/**
+ * Tests that annotation which enables alternative stereotype can be inherited
+ *
+ * @author Björn Kautler
+ */
+class EnableAlternativeStereotypeInheritanceTest extends BaseAlternativeStereotypeInheritanceTest {
+ @Inject
+ Foo foo
+
+ def 'inheritance for alternative stereotype annotation should work properly'() {
+ expect:
+ foo.ping() == FooAlternative.simpleName
+ }
+}
+
+@EnableWeld(automagic = true)
+@AddBeanClasses([Foo, FooAlternative])
+@EnableAlternativeStereotypes(SomeStereotype)
+class BaseAlternativeStereotypeInheritanceTest extends Specification {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/EnableAlternativeStereotypeTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/EnableAlternativeStereotypeTest.groovy
new file mode 100644
index 00000000..e5a1337d
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/EnableAlternativeStereotypeTest.groovy
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternativeStereotype
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.AddBeanClasses
+import org.jboss.weld.spock.auto.EnableAlternativeStereotypes
+import spock.lang.Specification
+
+/**
+ * Tests that alternative stereotype can be enabled via annotation.
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@AddBeanClasses([Foo, FooAlternative])
+@EnableAlternativeStereotypes(SomeStereotype)
+class EnableAlternativeStereotypeTest extends Specification {
+ @Inject
+ Foo foo
+
+ def 'enabled alternative stereotype should be enabled'() {
+ expect:
+ foo.ping() == FooAlternative.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/Foo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/Foo.groovy
new file mode 100644
index 00000000..c809bf1d
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/Foo.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternativeStereotype
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class Foo {
+ String ping() {
+ Foo.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/FooAlternative.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/FooAlternative.groovy
new file mode 100644
index 00000000..87e814d7
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/FooAlternative.groovy
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternativeStereotype
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@SomeStereotype
+@ApplicationScoped
+class FooAlternative extends Foo {
+ String ping() {
+ FooAlternative.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/SomeStereotype.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/SomeStereotype.groovy
new file mode 100644
index 00000000..7a6f089e
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternativeStereotype/SomeStereotype.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternativeStereotype
+
+import java.lang.annotation.Retention
+import java.lang.annotation.Target
+
+import jakarta.enterprise.inject.Alternative
+import jakarta.enterprise.inject.Stereotype
+
+import static java.lang.annotation.ElementType.TYPE
+import static java.lang.annotation.RetentionPolicy.RUNTIME
+
+/**
+ * @author Björn Kautler
+ */
+@Stereotype
+@Alternative
+@Retention(RUNTIME)
+@Target(TYPE)
+@interface SomeStereotype {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/EnableAlternativeTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/EnableAlternativeTest.groovy
new file mode 100644
index 00000000..873d9e55
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/EnableAlternativeTest.groovy
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternatives
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.AddBeanClasses
+import org.jboss.weld.spock.auto.EnableAlternatives
+import spock.lang.Specification
+
+/**
+ * Tests that @EnableAlternative works
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld(automagic = true)
+@EnableAlternatives(FooAlternative)
+@AddBeanClasses([Foo, FooAlternative])
+class EnableAlternativeTest extends Specification {
+ @Inject
+ Foo foo
+
+ def 'alternative should be enabled'() {
+ expect:
+ foo.ping() == FooAlternative.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/EnableAlternativesInheritanceTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/EnableAlternativesInheritanceTest.groovy
new file mode 100644
index 00000000..9d4a599a
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/EnableAlternativesInheritanceTest.groovy
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternatives
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.auto.AddBeanClasses
+import org.jboss.weld.spock.auto.EnableAlternatives
+import spock.lang.Specification
+
+/**
+ * Extends {@link BaseEnableAlternativeTest} hence inheriting the annotations. This tests that alternative enablement will be
+ * inherited.
+ *
+ * @author Björn Kautler
+ */
+class EnableAlternativesInheritanceTest extends BaseEnableAlternativeTest {
+ @Inject
+ Foo foo
+
+ def 'alternative enablement should be inherited'() {
+ expect:
+ foo.ping() == FooAlternative.simpleName
+ }
+}
+
+@EnableWeld(automagic = true)
+@AddBeanClasses([Foo, FooAlternative])
+@EnableAlternatives(FooAlternative)
+class BaseEnableAlternativeTest extends Specification {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/Foo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/Foo.groovy
new file mode 100644
index 00000000..e44815e4
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/Foo.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternatives
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class Foo {
+ String ping() {
+ Foo.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/FooAlternative.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/FooAlternative.groovy
new file mode 100644
index 00000000..1510b46a
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/alternatives/FooAlternative.groovy
@@ -0,0 +1,32 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.alternatives
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.enterprise.inject.Alternative
+
+/**
+ * @author Björn Kautler
+ */
+@Alternative
+@ApplicationScoped
+class FooAlternative extends Foo {
+ String ping() {
+ FooAlternative.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/Engine.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/Engine.groovy
new file mode 100644
index 00000000..15129669
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/Engine.groovy
@@ -0,0 +1,27 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.beans
+
+/**
+ * @author Björn Kautler
+ */
+interface Engine {
+ int getThrottle()
+
+ void setThrottle(int value)
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/V6.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/V6.groovy
new file mode 100644
index 00000000..aa63d8ca
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/V6.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.beans
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class V6 implements Engine, Serializable {
+ private int throttle = 0
+
+ int getThrottle() {
+ return throttle
+ }
+
+ @Override
+ void setThrottle(int throttle) {
+ this.throttle = throttle
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/V8.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/V8.groovy
new file mode 100644
index 00000000..151d9162
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/V8.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.beans
+
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class V8 implements Engine, Serializable {
+ private int throttle = 0
+
+ int getThrottle() {
+ return throttle
+ }
+
+ @Override
+ void setThrottle(int throttle) {
+ this.throttle = throttle
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/ConstructedV8NoAnnotation.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/ConstructedV8NoAnnotation.groovy
new file mode 100644
index 00000000..9ba3c3eb
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/ConstructedV8NoAnnotation.groovy
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.beans.unsatisfied
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class ConstructedV8NoAnnotation {
+ private V8NoAnnotation engine
+
+ @Inject
+ ConstructedV8NoAnnotation(V8NoAnnotation engine) {
+ this.engine = engine
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/InjectedV8NoAnnotation.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/InjectedV8NoAnnotation.groovy
new file mode 100644
index 00000000..000baa66
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/InjectedV8NoAnnotation.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.beans.unsatisfied
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class InjectedV8NoAnnotation {
+ @Inject
+ V8NoAnnotation v8
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/V8NoAnnotation.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/V8NoAnnotation.groovy
new file mode 100644
index 00000000..5bdb0ab1
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/beans/unsatisfied/V8NoAnnotation.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.beans.unsatisfied
+
+import org.jboss.weld.spock.auto.beans.Engine
+
+/**
+ * @author Björn Kautler
+ */
+// NOTE - deliberately missing bean defining annotation
+class V8NoAnnotation implements Engine, Serializable {
+ private int throttle = 0
+
+ int getThrottle() {
+ return throttle
+ }
+
+ @Override
+ void setThrottle(int throttle) {
+ this.throttle = throttle
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/extension/AddedExtension.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/extension/AddedExtension.groovy
new file mode 100644
index 00000000..4a57c723
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/extension/AddedExtension.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.extension
+
+import jakarta.enterprise.event.Observes
+import jakarta.enterprise.inject.spi.AfterBeanDiscovery
+import jakarta.enterprise.inject.spi.Extension
+
+/**
+ * @author Björn Kautler
+ */
+class AddedExtension implements Extension {
+ private static boolean enabled = false
+
+ void observeABD(@Observes AfterBeanDiscovery abd) {
+ enabled = true
+ }
+
+ static boolean isEnabled() {
+ enabled
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/DecoratedBean.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/DecoratedBean.groovy
new file mode 100644
index 00000000..deefb0b2
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/DecoratedBean.groovy
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.interceptorAndDecorator
+
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class DecoratedBean implements DecoratedBeanInterface {
+ @Override
+ String ping() {
+ DecoratedBean.toString()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/DecoratedBeanInterface.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/DecoratedBeanInterface.groovy
new file mode 100644
index 00000000..391f731f
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/DecoratedBeanInterface.groovy
@@ -0,0 +1,25 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.interceptorAndDecorator
+
+/**
+ * @author Björn Kautler
+ */
+interface DecoratedBeanInterface {
+ String ping()
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/InterceptedBean.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/InterceptedBean.groovy
new file mode 100644
index 00000000..0ac9d2ef
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/InterceptedBean.groovy
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.interceptorAndDecorator
+
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class InterceptedBean {
+ @TestInterceptorBinding
+ String ping() {
+ InterceptedBean.toString()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestDecorator.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestDecorator.groovy
new file mode 100644
index 00000000..4b31cb44
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestDecorator.groovy
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.interceptorAndDecorator
+
+import jakarta.decorator.Decorator
+import jakarta.decorator.Delegate
+import jakarta.enterprise.inject.Typed
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@Decorator
+// work-around for https://issues.redhat.com/browse/WELD-2713
+@Typed(DecoratedBeanInterface)
+class TestDecorator implements DecoratedBeanInterface {
+ @Inject
+ @Delegate
+ DecoratedBeanInterface delegate
+
+ @Override
+ String ping() {
+ TestDecorator.toString() + delegate.ping()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestInterceptor.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestInterceptor.groovy
new file mode 100644
index 00000000..b1cafb96
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestInterceptor.groovy
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.interceptorAndDecorator
+
+import jakarta.interceptor.AroundInvoke
+import jakarta.interceptor.Interceptor
+import jakarta.interceptor.InvocationContext
+
+/**
+ * @author Björn Kautler
+ */
+@Interceptor
+@TestInterceptorBinding
+class TestInterceptor {
+ @AroundInvoke
+ Object intercept(InvocationContext ctx) throws Exception {
+ TestInterceptor.toString() + ctx.proceed()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestInterceptorBinding.groovy b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestInterceptorBinding.groovy
new file mode 100644
index 00000000..435f25bb
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/auto/interceptorAndDecorator/TestInterceptorBinding.groovy
@@ -0,0 +1,36 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.auto.interceptorAndDecorator
+
+import java.lang.annotation.Retention
+import java.lang.annotation.Target
+
+import jakarta.interceptor.InterceptorBinding
+
+import static java.lang.annotation.ElementType.METHOD
+import static java.lang.annotation.ElementType.TYPE
+import static java.lang.annotation.RetentionPolicy.RUNTIME
+
+/**
+ * @author Björn Kautler
+ */
+@InterceptorBinding
+@Retention(RUNTIME)
+@Target([TYPE, METHOD])
+@interface TestInterceptorBinding {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/BeanManagerTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/BeanManagerTest.groovy
new file mode 100644
index 00000000..7fd969d5
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/BeanManagerTest.groovy
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class BeanManagerTest extends Specification {
+
+ @WeldSetup
+ def weld = WeldInitiator.of(Foo)
+
+ def 'BeanManager should be able to resolve added bean'() {
+ expect:
+ weld.beanManager.getBeans(Foo).size() == 1
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/ContainerNotRunningTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/ContainerNotRunningTest.groovy
new file mode 100644
index 00000000..f8ae9c1c
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/ContainerNotRunningTest.groovy
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+import static org.jboss.weld.spock.WeldInitiator.createWeld
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class ContainerNotRunningTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.of(createWeld().beanClasses(Foo))
+
+ def 'Weld should throw IllegalStateException after container was shut down'() {
+ given: 'Shutdown container manually'
+ weld.shutdown()
+
+ when:
+ weld.select(Foo).get()
+
+ then:
+ thrown(IllegalStateException)
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/CustomWeldTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/CustomWeldTest.groovy
new file mode 100644
index 00000000..5492cf48
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/CustomWeldTest.groovy
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+import static org.jboss.weld.spock.WeldInitiator.createWeld
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class CustomWeldTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.of(createWeld()
+ .alternatives(FooAlternative)
+ .beanClasses(Foo, FooAlternative))
+
+ def 'Weld should select registered alternative'() {
+ expect:
+ weld.select(Foo).get().bar == 'BAZ'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/EnableWeldTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/EnableWeldTest.groovy
new file mode 100644
index 00000000..769fa026
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/EnableWeldTest.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import jakarta.enterprise.inject.spi.BeanManager
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class EnableWeldTest extends Specification {
+ @Inject
+ BeanManager beanManager
+
+ def '@EnableWeld should initialize the Weld CDI container'() {
+ expect:
+ beanManager != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/Foo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/Foo.groovy
new file mode 100644
index 00000000..f5da68e9
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/Foo.groovy
@@ -0,0 +1,46 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import jakarta.annotation.PostConstruct
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class Foo implements IFoo {
+
+ private String bar
+
+ Foo() {
+ }
+
+ Foo(String bar) {
+ this.bar = bar
+ }
+
+ @PostConstruct
+ void init() {
+ bar = 'baz'
+ }
+
+ String getBar() {
+ return bar
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/FooAlternative.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/FooAlternative.groovy
new file mode 100644
index 00000000..c902ea10
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/FooAlternative.groovy
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import jakarta.enterprise.inject.Alternative
+
+/**
+ * @author Björn Kautler
+ */
+@Alternative
+class FooAlternative extends Foo {
+ @Override
+ String getBar() {
+ return super.bar.toUpperCase()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/IFoo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/IFoo.groovy
new file mode 100644
index 00000000..3f7cfb25
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/IFoo.groovy
@@ -0,0 +1,25 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+/**
+ * @author Björn Kautler
+ */
+interface IFoo {
+ String getBar()
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/SimpleTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/SimpleTest.groovy
new file mode 100644
index 00000000..139a20d4
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/SimpleTest.groovy
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import org.jboss.weld.spock.ofpackage.Alpha
+import spock.lang.Issue
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class SimpleTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.of(Foo)
+
+ def 'test Foo A'() {
+ expect:
+ weld.select(Foo).get().bar == 'baz'
+ !weld.select(Alpha).resolvable
+ }
+
+ @Issue('https://github.com/weld/weld-junit/issues/19')
+ void 'test Foo B'() {
+ expect:
+ weld.select(Foo).get().bar == 'baz'
+ !weld.select(Alpha).resolvable
+ }
+
+ def 'manual non-contextual injection should work properly'() {
+ given:
+ def sut = new NonContextual()
+
+ when:
+ def contextReleaser = weld.injectNonContextual(sut)
+
+ then:
+ sut.foo.bar == 'baz'
+
+ cleanup:
+ contextReleaser.close()
+ }
+
+ private static class NonContextual {
+ @Inject
+ private Foo foo
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/SomeFoo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/SomeFoo.groovy
new file mode 100644
index 00000000..9a298bc9
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/SomeFoo.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class SomeFoo {
+ @Inject
+ private Foo foo
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/SomeIFoo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/SomeIFoo.groovy
new file mode 100644
index 00000000..b8855cec
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/SomeIFoo.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class SomeIFoo {
+ @Inject
+ private IFoo foo
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/Baz.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/Baz.groovy
new file mode 100644
index 00000000..a08e64c0
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/Baz.groovy
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic.unsatisfied
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+import jakarta.inject.Named
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class Baz {
+ @Inject
+ @Named('bar-value')
+ String bar
+
+ Baz() {
+ }
+
+ String getBar() {
+ bar
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/FooDeps.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/FooDeps.groovy
new file mode 100644
index 00000000..8840fb31
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/FooDeps.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic.unsatisfied
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class FooDeps {
+ @Inject
+ Baz baz
+
+ FooDeps() {
+ }
+
+ String getBar() {
+ baz.bar
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/SomeFooDeps.groovy b/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/SomeFooDeps.groovy
new file mode 100644
index 00000000..06f4db47
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/basic/unsatisfied/SomeFooDeps.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.basic.unsatisfied
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class SomeFooDeps {
+ @Inject
+ private FooDeps fooDeps
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/AddBeanTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/AddBeanTest.groovy
new file mode 100644
index 00000000..85b2739c
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/AddBeanTest.groovy
@@ -0,0 +1,151 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import java.util.concurrent.atomic.AtomicInteger
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.enterprise.context.Dependent
+import jakarta.enterprise.util.TypeLiteral
+import org.jboss.weld.junit.MockBean
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Execution
+import spock.lang.Isolated
+import spock.lang.Rollup
+import spock.lang.Shared
+import spock.lang.Specification
+
+import static org.spockframework.runtime.model.parallel.ExecutionMode.SAME_THREAD
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class AddBeanTest extends Specification {
+ @Shared
+ def SEQUENCE = new AtomicInteger(0)
+
+ MyService myServiceMock = Mock()
+
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(Blue)
+ .addBeans(MockBean.of(myServiceMock, MyService))
+ .addBeans(MockBean.read(BlueToDiscover).scope(Dependent).build())
+ .addBeans(createListBean(), createSequenceBean(), createIdSupplierBean())
+ .build()
+
+ def createListBean() {
+ MockBean
+ .builder()
+ .types(new TypeLiteral>() {}.type)
+ .qualifiers(Meaty.Literal.INSTANCE)
+ .creating(Stub(List) {
+ get(0) >> '42'
+ })
+ .build()
+ }
+
+ def createSequenceBean() {
+ MockBean
+ .builder()
+ .types(Integer)
+ .qualifiers(Meaty.Literal.INSTANCE)
+ .create { SEQUENCE.incrementAndGet() }
+ .build()
+ }
+
+ def createIdSupplierBean() {
+ MockBean
+ .builder()
+ .types(IdSupplier)
+ .scope(ApplicationScoped)
+ .create { new IdSupplier(UUID.randomUUID().toString()) }
+ .build()
+ }
+
+ interface MyService {
+ void doBusiness(String name)
+ }
+
+ static class IdSupplier {
+ private final String id
+
+ IdSupplier(String id) {
+ this.id = id
+ }
+
+ String getId() {
+ return id
+ }
+ }
+}
+
+class ConcurrentAddBeanTest extends AddBeanTest {
+ def 'Blue should get @Meaty List injected'() {
+ expect:
+ weld.select(Blue).get().stringList.get(0) == '42'
+ }
+
+ @Rollup
+ @Execution(SAME_THREAD)
+ def 'each Bean.create() should increment the sequence'() {
+ expect:
+ weld.select(Integer, Meaty.Literal.INSTANCE).get() == i
+
+ where:
+ i << (1..10)
+ }
+
+ def 'mocking on an injected mock bean should work properly'() {
+ given:
+ def myService = weld.select(MyService).get()
+
+ when:
+ myService.doBusiness('Adalbert')
+
+ then:
+ 1 * myServiceMock.doBusiness(_)
+ }
+
+ def 'application scoped beans should work properly'() {
+ when:
+ def first = weld.select(IdSupplier).get()
+ def second = weld.select(IdSupplier).get()
+
+ then:
+ first.id == second.id
+ }
+}
+
+@Isolated
+class IsolatedAddBeanTest extends AddBeanTest {
+ def 'the scope of "read" beans should be taken from the manual override'() {
+ when:
+ def blue1 = weld.select(BlueToDiscover).get()
+ def blue2 = weld.select(BlueToDiscover).get()
+
+ then:
+ blue1.id != null
+ blue2.id != null
+ blue1.id != blue2.id
+ weld.beanManager.getBeans('blue').size() == 1
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/AddGloballyEnabledAlternativeTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/AddGloballyEnabledAlternativeTest.groovy
new file mode 100644
index 00000000..cfd080cd
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/AddGloballyEnabledAlternativeTest.groovy
@@ -0,0 +1,88 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import jakarta.enterprise.inject.spi.Bean
+import jakarta.enterprise.util.TypeLiteral
+import org.jboss.weld.junit.MockBean
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Isolated
+import spock.lang.Specification
+
+/**
+ * Tests {@link org.jboss.weld.junit.MockBean} adding a bean that is a globally enabled alternative.
+ *
+ * @author Björn Kautler
+ */
+@Isolated
+@EnableWeld
+class AddGloballyEnabledAlternativeTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(Bar)
+ .addBeans(createFooAlternativeBean(), createListBean())
+ .build()
+
+ static Bean> createFooAlternativeBean() {
+ return MockBean
+ .read(Foo)
+ .priority(3)
+ .alternative(true)
+ .addQualifier(Meaty.Literal.INSTANCE)
+ .build()
+ }
+
+ Bean> createListBean() {
+ return MockBean
+ .builder()
+ .types(new TypeLiteral>() {}.type)
+ .globallySelectedAlternative(2)
+ .creating(Stub(List) {
+ get(0) >> '42'
+ })
+ .build()
+ }
+
+ def 'all alternative beans should be added'() {
+ when:
+ def bar = weld.select(Bar).get()
+
+ then:
+ bar.foo != null
+ bar.someList != null
+ bar.someList.get(0) == '42'
+ }
+
+ def 'all alternative beans can be selected'() {
+ when:
+ def beans = weld.beanManager.getBeans(Foo, Meaty.Literal.INSTANCE)
+
+ then:
+ beans.size() == 1
+ beans.first().alternative
+
+ when:
+ beans = weld.beanManager.getBeans(new TypeLiteral>() {}.type)
+
+ then:
+ beans.size() == 1
+ beans.first().alternative
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/AddPassivatingBeanTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/AddPassivatingBeanTest.groovy
new file mode 100644
index 00000000..65a52d08
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/AddPassivatingBeanTest.groovy
@@ -0,0 +1,56 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import jakarta.enterprise.context.SessionScoped
+import jakarta.enterprise.inject.spi.Bean
+import jakarta.enterprise.util.TypeLiteral
+import org.jboss.weld.junit.MockBean
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class AddPassivatingBeanTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(List)
+ .addBeans(createListBean())
+ .activate(SessionScoped)
+ .build()
+
+ Bean> createListBean() {
+ return MockBean
+ .builder()
+ .types(new TypeLiteral>() {}.type)
+ .scope(SessionScoped)
+ .creating(Stub(List) {
+ get(0) >> '42'
+ })
+ .build()
+ }
+
+ def 'passivating bean should work properly'() {
+ expect:
+ weld.select(new TypeLiteral>() {}).get().get(0) == '42'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/AlternativeMockBeanTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/AlternativeMockBeanTest.groovy
new file mode 100644
index 00000000..bc9e6489
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/AlternativeMockBeanTest.groovy
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import jakarta.enterprise.context.Dependent
+import org.jboss.weld.junit.MockBean
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class AlternativeMockBeanTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(SimpleService)
+ .addBeans(MockBean
+ .builder()
+ .types(MyService)
+ .selectedAlternative()
+ .beanClass(CoolService)
+ .create { new CoolService() }
+ .build())
+ .build()
+
+ def 'alternative bean should be selected'() {
+ expect:
+ weld.select(MyService).get().doBusiness() == 1000
+ }
+
+ interface MyService {
+ int doBusiness()
+ }
+
+ @Dependent
+ static class SimpleService implements MyService {
+ @Override
+ int doBusiness() {
+ return 0
+ }
+ }
+
+ static class CoolService implements MyService {
+ @Override
+ int doBusiness() {
+ return 1000
+ }
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/Bar.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/Bar.groovy
new file mode 100644
index 00000000..1eadf91a
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/Bar.groovy
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class Bar {
+ @Inject
+ @Meaty
+ private Foo foo
+
+ @Inject
+ private List someList
+
+ List getSomeList() {
+ return someList
+ }
+
+ Foo getFoo() {
+ return foo
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/Blue.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/Blue.groovy
new file mode 100644
index 00000000..7d66678a
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/Blue.groovy
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Inject
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class Blue {
+ @Inject
+ @Meaty
+ private List stringList
+
+ List getStringList() {
+ return stringList
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/BlueToDiscover.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/BlueToDiscover.groovy
new file mode 100644
index 00000000..38a29af8
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/BlueToDiscover.groovy
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import jakarta.annotation.PostConstruct
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.inject.Named
+
+/**
+ * @author Björn Kautler
+ */
+@Named('blue')
+@ApplicationScoped
+class BlueToDiscover {
+ private String id
+
+ @PostConstruct
+ void init() {
+ this.id = UUID.randomUUID().toString()
+ }
+
+ String getId() {
+ return id
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/Foo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/Foo.groovy
new file mode 100644
index 00000000..3473d2bc
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/Foo.groovy
@@ -0,0 +1,27 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+/**
+ * @author Björn Kautler
+ */
+class Foo {
+ String ping() {
+ 'foo'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/Meaty.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/Meaty.groovy
new file mode 100644
index 00000000..3c3617c5
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/Meaty.groovy
@@ -0,0 +1,47 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import java.lang.annotation.Retention
+import java.lang.annotation.Target
+
+import jakarta.enterprise.util.AnnotationLiteral
+import jakarta.inject.Qualifier
+
+import static java.lang.annotation.ElementType.FIELD
+import static java.lang.annotation.ElementType.METHOD
+import static java.lang.annotation.ElementType.PARAMETER
+import static java.lang.annotation.ElementType.TYPE
+import static java.lang.annotation.RetentionPolicy.RUNTIME
+
+/**
+ * @author Björn Kautler
+ */
+@Qualifier
+@Target([TYPE, METHOD, PARAMETER, FIELD])
+@Retention(RUNTIME)
+@interface Meaty {
+ static class Literal extends AnnotationLiteral implements Meaty {
+ private static final long serialVersionUID = 1L
+
+ public static final Meaty INSTANCE = new Literal()
+
+ private Literal() {
+ }
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/MockBeanWithQualifiersTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/MockBeanWithQualifiersTest.groovy
new file mode 100644
index 00000000..9481df21
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/MockBeanWithQualifiersTest.groovy
@@ -0,0 +1,49 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import jakarta.enterprise.inject.Any
+import org.jboss.weld.junit.MockBean
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * Tests that {@link MockBean} with custom qualifiers has {@link Any} qualifier automatically added.
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld
+class MockBeanWithQualifiersTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(AlternativeMockBeanTest.SimpleService)
+ .addBeans(MockBean
+ .builder()
+ .types(String)
+ .qualifiers(Meaty.Literal.INSTANCE)
+ .create { 'foo' }
+ .build())
+ .build()
+
+ void 'mock beans with custom qualifiers should have the Any qualifier automatically'() {
+ expect:
+ weld.select(String, Any.Literal.INSTANCE).get() == 'foo'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/TestClassProducerTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/TestClassProducerTest.groovy
new file mode 100644
index 00000000..495cce7b
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/TestClassProducerTest.groovy
@@ -0,0 +1,50 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.enterprise.inject.Produces
+import jakarta.enterprise.util.TypeLiteral
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * Note that we add the class to the deployment so it is recognized as a bean and therefore the producer is also discovered.
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld
+class TestClassProducerTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.from(List, TestClassProducerTest).build()
+
+ def 'bean from producer method should be found'() {
+ expect:
+ weld.select(new TypeLiteral>() {}).get().get(0) == '42'
+ }
+
+ @ApplicationScoped
+ @Produces
+ List produceList() {
+ Stub(List) {
+ get(0) >> '42'
+ }
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/AddExtensionAsBeanClassWithFromMethodTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/AddExtensionAsBeanClassWithFromMethodTest.groovy
new file mode 100644
index 00000000..c6d685ee
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/AddExtensionAsBeanClassWithFromMethodTest.groovy
@@ -0,0 +1,44 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean.extension
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class AddExtensionAsBeanClassWithFromMethodTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.from(GoodOldBean, MyExtension).build()
+
+ def 'class added with from should be recognized as extension'() {
+ expect: 'GoodOldBean should be resolvable'
+ weld.select(GoodOldBean).resolvable
+
+ when:
+ def extInstance = weld.select(MyExtension)
+
+ then: 'extension should be resolvable and should have observed the other bean'
+ extInstance.resolvable
+ extInstance.get().beanObserved()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/AddExtensionAsBeanClassWithOfMethodTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/AddExtensionAsBeanClassWithOfMethodTest.groovy
new file mode 100644
index 00000000..df582bc8
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/AddExtensionAsBeanClassWithOfMethodTest.groovy
@@ -0,0 +1,44 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean.extension
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class AddExtensionAsBeanClassWithOfMethodTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.of(GoodOldBean, MyExtension)
+
+ def 'class added with of should be recognized as extension'() {
+ expect: 'GoodOldBean should be resolvable'
+ weld.select(GoodOldBean).resolvable
+
+ when:
+ def extInstance = weld.select(MyExtension)
+
+ then: 'extension should be resolvable and should have observed the other bean'
+ extInstance.resolvable
+ extInstance.get().beanObserved()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/GoodOldBean.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/GoodOldBean.groovy
new file mode 100644
index 00000000..65c3ed59
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/GoodOldBean.groovy
@@ -0,0 +1,27 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean.extension
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class GoodOldBean {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/MyExtension.groovy b/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/MyExtension.groovy
new file mode 100644
index 00000000..12aac3a3
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/bean/extension/MyExtension.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.bean.extension
+
+import jakarta.enterprise.event.Observes
+import jakarta.enterprise.inject.spi.Extension
+import jakarta.enterprise.inject.spi.ProcessAnnotatedType
+
+/**
+ * @author Björn Kautler
+ */
+class MyExtension implements Extension {
+ private boolean beanObserved = false
+
+ void observePAT(@Observes ProcessAnnotatedType event) {
+ beanObserved = true
+ }
+
+ boolean beanObserved() {
+ return beanObserved
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/contexts/ContextsActivatedTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/contexts/ContextsActivatedTest.groovy
new file mode 100644
index 00000000..322cbc91
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/contexts/ContextsActivatedTest.groovy
@@ -0,0 +1,48 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.contexts
+
+import jakarta.enterprise.context.RequestScoped
+import jakarta.enterprise.context.SessionScoped
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class ContextsActivatedTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(Foo, Oof, RequestScopedProducer)
+ .activate(RequestScoped, SessionScoped)
+ .build()
+
+ @Inject
+ String producedString
+
+ def 'normal scopes should behave properly'() {
+ expect:
+ weld.select(Foo).get().id == weld.select(Foo).get().id
+ weld.select(Oof).get().id == weld.select(Oof).get().id
+ producedString == 'foo'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/contexts/Foo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/contexts/Foo.groovy
new file mode 100644
index 00000000..5e4e6bcb
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/contexts/Foo.groovy
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.contexts
+
+import jakarta.annotation.PostConstruct
+import jakarta.enterprise.context.RequestScoped
+
+/**
+ * @author Björn Kautler
+ */
+@RequestScoped
+class Foo {
+ private String id
+
+ @PostConstruct
+ void init() {
+ id = UUID.randomUUID().toString()
+ }
+
+ String getId() {
+ return id
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/contexts/InvalidScopeTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/contexts/InvalidScopeTest.groovy
new file mode 100644
index 00000000..07e5d241
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/contexts/InvalidScopeTest.groovy
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.contexts
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class InvalidScopeTest extends Specification {
+ def 'activating a non-scope annotation should throw IllegalArgumentException'() {
+ when:
+ // the container doesn't really even start here and it's not proper way to start it
+ // but it's sufficient to check that only scopes can be activated
+ WeldInitiator.from(Foo).activate(SomeAnnotation)
+
+ then:
+ thrown(IllegalArgumentException)
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/contexts/Oof.groovy b/spock/src/test/groovy/org/jboss/weld/spock/contexts/Oof.groovy
new file mode 100644
index 00000000..5ebaa74f
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/contexts/Oof.groovy
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.contexts
+
+import jakarta.annotation.PostConstruct
+import jakarta.enterprise.context.SessionScoped
+
+/**
+ * @author Björn Kautler
+ */
+@SessionScoped
+class Oof implements Serializable {
+ private String id
+
+ @PostConstruct
+ void init() {
+ id = UUID.randomUUID().toString()
+ }
+
+ String getId() {
+ return id
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/contexts/RequestScopedProducer.groovy b/spock/src/test/groovy/org/jboss/weld/spock/contexts/RequestScopedProducer.groovy
new file mode 100644
index 00000000..b0559f11
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/contexts/RequestScopedProducer.groovy
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.contexts
+
+import jakarta.enterprise.context.Dependent
+import jakarta.enterprise.context.RequestScoped
+import jakarta.enterprise.inject.Produces
+
+/**
+ * @author Björn Kautler
+ */
+@RequestScoped
+class RequestScopedProducer {
+ @Produces
+ @Dependent
+ String produceDependentString() {
+ return 'foo'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/contexts/SomeAnnotation.groovy b/spock/src/test/groovy/org/jboss/weld/spock/contexts/SomeAnnotation.groovy
new file mode 100644
index 00000000..aee3d65e
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/contexts/SomeAnnotation.groovy
@@ -0,0 +1,29 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.contexts
+
+import java.lang.annotation.Retention
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME
+
+/**
+ * @author Björn Kautler
+ */
+@Retention(RUNTIME)
+@interface SomeAnnotation {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/contexts/events/ContextLifecycleEventsObserver.groovy b/spock/src/test/groovy/org/jboss/weld/spock/contexts/events/ContextLifecycleEventsObserver.groovy
new file mode 100644
index 00000000..5d2fe3af
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/contexts/events/ContextLifecycleEventsObserver.groovy
@@ -0,0 +1,51 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.contexts.events
+
+import java.util.concurrent.CopyOnWriteArrayList
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.enterprise.context.Destroyed
+import jakarta.enterprise.context.Initialized
+import jakarta.enterprise.context.RequestScoped
+import jakarta.enterprise.context.SessionScoped
+import jakarta.enterprise.event.Observes
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class ContextLifecycleEventsObserver {
+ static final List EVENTS = new CopyOnWriteArrayList<>()
+
+ void onRequestContextInit(@Observes @Initialized(RequestScoped) event) {
+ EVENTS.add(Initialized.name + RequestScoped.name)
+ }
+
+ void onRequestContextDestroy(@Observes @Destroyed(RequestScoped) event) {
+ EVENTS.add(Destroyed.name + RequestScoped.name)
+ }
+
+ void onSessionContextInit(@Observes @Initialized(SessionScoped) event) {
+ EVENTS.add(Initialized.name + SessionScoped.name)
+ }
+
+ void onSessionContextDestroy(@Observes @Destroyed(SessionScoped) event) {
+ EVENTS.add(Destroyed.name + SessionScoped.name)
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/contexts/events/ContextLifecycleEventsTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/contexts/events/ContextLifecycleEventsTest.groovy
new file mode 100644
index 00000000..cc869231
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/contexts/events/ContextLifecycleEventsTest.groovy
@@ -0,0 +1,51 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.contexts.events
+
+import jakarta.enterprise.context.Destroyed
+import jakarta.enterprise.context.Initialized
+import jakarta.enterprise.context.RequestScoped
+import jakarta.enterprise.context.SessionScoped
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class ContextLifecycleEventsTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(ContextLifecycleEventsObserver)
+ .activate(RequestScoped, SessionScoped)
+ .build()
+
+ def 'scope initialization events were fired when booting up container'() {
+ expect:
+ ContextLifecycleEventsObserver.EVENTS.contains(Initialized.name + RequestScoped.name)
+ ContextLifecycleEventsObserver.EVENTS.contains(Initialized.name + SessionScoped.name)
+ }
+
+ def cleanupSpec() {
+ // At this time @Destroyed from the previous test method should be fired
+ assert ContextLifecycleEventsObserver.EVENTS.contains(Destroyed.name + RequestScoped.name)
+ assert ContextLifecycleEventsObserver.EVENTS.contains(Destroyed.name + SessionScoped.name)
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/enricher/FooWeldSpockEnricher.groovy b/spock/src/test/groovy/org/jboss/weld/spock/enricher/FooWeldSpockEnricher.groovy
new file mode 100644
index 00000000..c0d41c2b
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/enricher/FooWeldSpockEnricher.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.enricher
+
+import org.jboss.weld.environment.se.Weld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSpockEnricher
+import org.jboss.weld.spock.basic.Foo
+import org.jboss.weld.spock.enricher.disabled.WeldSpockEnricherDisabledTest
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+class FooWeldSpockEnricher implements WeldSpockEnricher {
+ @Override
+ void enrich(Specification testInstance, Weld weld, WeldInitiator.Builder weldInitiatorBuilder) {
+ if (testInstance?.getClass() in [WeldSpockEnricherTest, WeldSpockEnricherDisabledTest]) {
+ weld.addBeanClass(Foo)
+ }
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/enricher/WeldSpockEnricherTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/enricher/WeldSpockEnricherTest.groovy
new file mode 100644
index 00000000..85a0d2ba
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/enricher/WeldSpockEnricherTest.groovy
@@ -0,0 +1,42 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.enricher
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.Foo
+import spock.lang.ResourceLock
+import spock.lang.Specification
+
+import static org.spockframework.runtime.model.parallel.ResourceAccessMode.READ
+import static org.spockframework.runtime.model.parallel.Resources.SYSTEM_PROPERTIES
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+@ResourceLock(value = SYSTEM_PROPERTIES, mode = READ)
+class WeldSpockEnricherTest extends Specification {
+ @Inject
+ Foo foo
+
+ def 'weld spock enricher should do its work'() {
+ expect:
+ foo.bar == 'baz'
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/enricher/disabled/WeldSpockEnricherDisabledTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/enricher/disabled/WeldSpockEnricherDisabledTest.groovy
new file mode 100644
index 00000000..6d48fef0
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/enricher/disabled/WeldSpockEnricherDisabledTest.groovy
@@ -0,0 +1,51 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.enricher.disabled
+
+import jakarta.enterprise.inject.Instance
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.basic.Foo
+import org.jboss.weld.spock.enricher.FooWeldSpockEnricher
+import spock.lang.ResourceLock
+import spock.lang.Specification
+
+import static org.spockframework.runtime.model.parallel.Resources.SYSTEM_PROPERTIES
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+@ResourceLock(SYSTEM_PROPERTIES)
+class WeldSpockEnricherDisabledTest extends Specification {
+ @Inject
+ Instance foo
+
+ def setupSpec() {
+ System.setProperty(FooWeldSpockEnricher.name, 'false')
+ }
+
+ def cleanupSpec() {
+ System.clearProperty(FooWeldSpockEnricher.name)
+ }
+
+ void testCustomizer() {
+ expect:
+ foo.unsatisfied
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/event/DummyObserver.groovy b/spock/src/test/groovy/org/jboss/weld/spock/event/DummyObserver.groovy
new file mode 100644
index 00000000..8f35864b
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/event/DummyObserver.groovy
@@ -0,0 +1,36 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.event
+
+import java.util.concurrent.CopyOnWriteArrayList
+
+import jakarta.enterprise.context.ApplicationScoped
+import jakarta.enterprise.event.Observes
+import org.jboss.weld.spock.basic.Foo
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class DummyObserver {
+ static final List MESSAGES = new CopyOnWriteArrayList<>()
+
+ void observeHelloMessage(@Observes Foo message) {
+ MESSAGES.add(message)
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/event/FireEventTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/event/FireEventTest.groovy
new file mode 100644
index 00000000..7f7ffc3a
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/event/FireEventTest.groovy
@@ -0,0 +1,45 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.event
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import org.jboss.weld.spock.basic.Foo
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class FireEventTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.of(DummyObserver)
+
+ def 'event should be fired'() {
+ given:
+ DummyObserver.MESSAGES.clear()
+
+ when: 'Fire an event'
+ weld.event().select(Foo).fire(new Foo())
+
+ then:
+ DummyObserver.MESSAGES.size() == 1
+ DummyObserver.MESSAGES.first().bar == null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/Bar.groovy b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/Bar.groovy
new file mode 100644
index 00000000..2262327c
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/Bar.groovy
@@ -0,0 +1,39 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.explicitInjection
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class Bar {
+ private String someText
+
+ Bar() {
+ }
+
+ Bar(String someText) {
+ this.someText = someText
+ }
+
+ String ping() {
+ return someText
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/BeanWithQualifier.groovy b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/BeanWithQualifier.groovy
new file mode 100644
index 00000000..341b788e
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/BeanWithQualifier.groovy
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.explicitInjection
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+@MyQualifier
+class BeanWithQualifier {
+ String ping() {
+ return BeanWithQualifier.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/CustomExtension.groovy b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/CustomExtension.groovy
new file mode 100644
index 00000000..25c5cdb3
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/CustomExtension.groovy
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.explicitInjection
+
+import java.lang.annotation.Retention
+import java.lang.annotation.Target
+
+import org.spockframework.runtime.extension.ExtensionAnnotation
+import org.spockframework.runtime.extension.IAnnotationDrivenExtension
+import org.spockframework.runtime.model.FeatureInfo
+
+import static java.lang.annotation.ElementType.METHOD
+import static java.lang.annotation.RetentionPolicy.RUNTIME
+import static org.spockframework.runtime.model.MethodInfo.MISSING_ARGUMENT
+
+/**
+ * Custom extension just to verify a case where we cannot handle some types.
+ *
+ * @author Björn Kautler
+ */
+class CustomExtension implements IAnnotationDrivenExtension {
+ @Override
+ void visitFeatureAnnotation(Custom custom, FeatureInfo feature) {
+ Class>[] parameterTypes = feature.featureMethod.reflection.parameterTypes
+ feature.featureMethod.addInterceptor {
+ it.arguments.eachWithIndex { argument, i ->
+ if ((argument == MISSING_ARGUMENT) && (parameterTypes[i] == Bar)) {
+ it.arguments[i] = new Bar(CustomExtension.simpleName)
+ }
+ }
+ }
+ }
+}
+
+@Retention(RUNTIME)
+@Target(METHOD)
+@ExtensionAnnotation(CustomExtension)
+@interface Custom {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/ExplicitParameterInjectionViaClassAnnotationTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/ExplicitParameterInjectionViaClassAnnotationTest.groovy
new file mode 100644
index 00000000..84843ff6
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/ExplicitParameterInjectionViaClassAnnotationTest.groovy
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.explicitInjection
+
+import jakarta.enterprise.inject.Default
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld(explicitParamInjection = true)
+class ExplicitParameterInjectionViaClassAnnotationTest extends Specification {
+ @Custom
+ void 'parameters with qualifiers should be injected'(@Default Foo foo, Bar bar, @MyQualifier BeanWithQualifier bean) {
+ expect: 'Bar should be resolved by another extension'
+ bar.ping() == CustomExtension.simpleName
+
+ and: 'Foo should be resolved as usual'
+ foo.ping() == Foo.simpleName
+
+ and: 'BeanWithQualifier should be resolved'
+ bean.ping() == BeanWithQualifier.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/ExplicitParameterInjectionViaMethodAnnotationTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/ExplicitParameterInjectionViaMethodAnnotationTest.groovy
new file mode 100644
index 00000000..96b246ef
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/ExplicitParameterInjectionViaMethodAnnotationTest.groovy
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.explicitInjection
+
+import jakarta.enterprise.inject.Default
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+class ExplicitParameterInjectionViaMethodAnnotationTest extends Specification {
+ @EnableWeld(explicitParamInjection = true)
+ @Custom
+ void 'parameters with qualifiers should be injected'(@Default Foo foo, Bar bar, @MyQualifier BeanWithQualifier bean) {
+ expect: 'Bar should be resolved by another extension'
+ bar.ping() == CustomExtension.simpleName
+
+ and: 'Foo should be resolved as usual'
+ foo.ping() == Foo.simpleName
+
+ and: 'BeanWithQualifier should be resolved'
+ bean.ping() == BeanWithQualifier.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/Foo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/Foo.groovy
new file mode 100644
index 00000000..4ee79a6b
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/Foo.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.explicitInjection
+
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class Foo {
+ String ping() {
+ return Foo.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/MyQualifier.groovy b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/MyQualifier.groovy
new file mode 100644
index 00000000..3c3136b5
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/explicitInjection/MyQualifier.groovy
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.explicitInjection
+
+import java.lang.annotation.Retention
+import java.lang.annotation.Target
+
+import jakarta.inject.Qualifier
+
+import static java.lang.annotation.ElementType.FIELD
+import static java.lang.annotation.ElementType.METHOD
+import static java.lang.annotation.ElementType.PARAMETER
+import static java.lang.annotation.ElementType.TYPE
+import static java.lang.annotation.RetentionPolicy.RUNTIME
+
+/**
+ * @author Björn Kautler
+ */
+@Qualifier
+@Retention(RUNTIME)
+@Target([FIELD, METHOD, TYPE, PARAMETER])
+@interface MyQualifier {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/BarBean.groovy b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/BarBean.groovy
new file mode 100644
index 00000000..cc0a5b1b
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/BarBean.groovy
@@ -0,0 +1,31 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.extensionInjection
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+@MyQualifier
+class BarBean {
+ String ping() {
+ return BarBean.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/FooBean.groovy b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/FooBean.groovy
new file mode 100644
index 00000000..38717347
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/FooBean.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.extensionInjection
+
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class FooBean {
+ String ping() {
+ return FooBean.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/MyQualifier.groovy b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/MyQualifier.groovy
new file mode 100644
index 00000000..107fe2e4
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/MyQualifier.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.extensionInjection
+
+import java.lang.annotation.Retention
+import java.lang.annotation.Target
+
+import jakarta.inject.Qualifier
+
+import static java.lang.annotation.ElementType.FIELD
+import static java.lang.annotation.ElementType.PARAMETER
+import static java.lang.annotation.ElementType.TYPE
+import static java.lang.annotation.RetentionPolicy.RUNTIME
+
+/**
+ * @author Björn Kautler
+ */
+@Qualifier
+@Target([FIELD, PARAMETER, TYPE])
+@Retention(RUNTIME)
+@interface MyQualifier {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/SomeBean.groovy b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/SomeBean.groovy
new file mode 100644
index 00000000..adca5105
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/SomeBean.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.extensionInjection
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class SomeBean {
+ String ping() {
+ return SomeBean.simpleName
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/SpockInjectionTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/SpockInjectionTest.groovy
new file mode 100644
index 00000000..71a3a499
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/extensionInjection/SpockInjectionTest.groovy
@@ -0,0 +1,125 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.extensionInjection
+
+import jakarta.inject.Inject
+import org.jboss.weld.spock.EnableWeld
+import spock.lang.Execution
+import spock.lang.Rollup
+import spock.lang.Shared
+import spock.lang.Specification
+
+import static org.jboss.weld.spock.EnableWeld.Scope.FEATURE
+import static org.jboss.weld.spock.EnableWeld.Scope.SPECIFICATION
+import static org.spockframework.runtime.model.parallel.ExecutionMode.SAME_THREAD
+
+/**
+ * Basic test for Spock injection into parameter/field handled by Weld
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld
+@Execution(SAME_THREAD)
+class SpockInjectionTest extends Specification {
+ @Inject
+ SomeBean bean
+
+ @Inject
+ @Shared
+ SomeBean sharedBean
+
+ def setupBean
+
+ def setup(SomeBean setupBean) {
+ this.setupBean = setupBean
+ }
+
+ def 'field should be injected'() {
+ expect:
+ bean != null
+ bean.ping()
+ }
+
+ def 'shared field should be injected with scope ITERATION'() {
+ expect:
+ sharedBean != null
+ sharedBean.ping()
+ }
+
+ @EnableWeld(scope = FEATURE)
+ def 'shared field should be injected with scope FEATURE'() {
+ expect:
+ sharedBean != null
+ sharedBean.ping()
+ }
+
+ @Rollup
+ @EnableWeld(scope = FEATURE)
+ def 'shared field should be injected with scope FEATURE for data-driven feature'() {
+ expect:
+ sharedBean != null
+ sharedBean.ping()
+
+ where:
+ i = 1
+ }
+
+ def 'parameter should be injected'(FooBean foo) {
+ expect:
+ foo != null
+ foo.ping()
+ }
+
+ def 'parameter with qualifier should be injected'(@MyQualifier BarBean bar) {
+ expect:
+ bar != null
+ bar.ping()
+ }
+
+ def 'parameter should be injected into setup method'() {
+ expect:
+ setupBean != null
+ setupBean.ping()
+ }
+}
+
+@EnableWeld(scope = SPECIFICATION)
+class SpecificationSpockInjectionTest extends Specification {
+ @Inject
+ @Shared
+ SomeBean sharedBean
+
+ @Shared
+ def setupSpecBean
+
+ def setupSpec(SomeBean setupSpecBean) {
+ this.setupSpecBean = setupSpecBean
+ }
+
+ def 'shared field should be injected with scope SPECIFICATION'() {
+ expect:
+ sharedBean != null
+ sharedBean.ping()
+ }
+
+ def 'parameter should be injected into setupSpec method with scope SPECIFICATION'() {
+ expect:
+ setupSpecBean != null
+ setupSpecBean.ping()
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/initiator/bean/Foo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/initiator/bean/Foo.groovy
new file mode 100644
index 00000000..6e02e214
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/initiator/bean/Foo.groovy
@@ -0,0 +1,24 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.initiator.bean
+
+/**
+ * @author Björn Kautler
+ */
+class Foo {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/PrivateWeldInitiatorTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/PrivateWeldInitiatorTest.groovy
new file mode 100644
index 00000000..8dca2d2e
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/PrivateWeldInitiatorTest.groovy
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.initiator.discovery
+
+import org.jboss.weld.environment.se.Weld
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import org.jboss.weld.spock.initiator.bean.Foo
+import spock.lang.Specification
+
+/**
+ * Tests a case where WeldInitiator is a private field in a test class
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld
+class PrivateWeldInitiatorTest extends Specification {
+ @WeldSetup
+ private weld = WeldInitiator.of(new Weld().addBeanClass(Foo))
+
+ def 'Weld can be initialized from an initiator in a private field'() {
+ expect:
+ weld.select(Foo).get() != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/ProtectedWeldInitiatorInSuperclassTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/ProtectedWeldInitiatorInSuperclassTest.groovy
new file mode 100644
index 00000000..84ed02a8
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/ProtectedWeldInitiatorInSuperclassTest.groovy
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.initiator.discovery
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.initiator.bean.Foo
+
+/**
+ * Tests a case where WeldInitiator comes from a protected field in a superclass
+ *
+ * @author Björn Kautler
+ */
+@EnableWeld
+class ProtectedWeldInitiatorInSuperclassTest extends SuperclassWithProtectedWeldInitiator {
+ def 'Weld can be initialized from an initiator in a protected field of a super spec'() {
+ expect:
+ weld.select(Foo).get() != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithProtectedWeldInitiator.groovy b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithProtectedWeldInitiator.groovy
new file mode 100644
index 00000000..d03d6f96
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithProtectedWeldInitiator.groovy
@@ -0,0 +1,32 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.initiator.discovery
+
+import org.jboss.weld.environment.se.Weld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import org.jboss.weld.spock.initiator.bean.Foo
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+class SuperclassWithProtectedWeldInitiator extends Specification {
+ @WeldSetup
+ protected weld = WeldInitiator.of(new Weld().addBeanClass(Foo))
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithWeldInitiator.groovy b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithWeldInitiator.groovy
new file mode 100644
index 00000000..f9277b0b
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithWeldInitiator.groovy
@@ -0,0 +1,34 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.initiator.discovery
+
+import org.jboss.weld.environment.se.Weld
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import org.jboss.weld.spock.initiator.bean.Foo
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+abstract class SuperclassWithWeldInitiator extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.of(new Weld().addBeanClass(Foo))
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithWeldInitiatorTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithWeldInitiatorTest.groovy
new file mode 100644
index 00000000..8af59e23
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/initiator/discovery/SuperclassWithWeldInitiatorTest.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.initiator.discovery
+
+import org.jboss.weld.spock.initiator.bean.Foo
+
+/**
+ * @author Björn Kautler
+ */
+class SuperclassWithWeldInitiatorTest extends SuperclassWithWeldInitiator {
+ def 'Weld can be initialized from an initiator in a super spec'() {
+ expect:
+ weld.select(Foo).get() != null
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/interceptor/MockInterceptorTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/interceptor/MockInterceptorTest.groovy
new file mode 100644
index 00000000..fc70afa2
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/interceptor/MockInterceptorTest.groovy
@@ -0,0 +1,121 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.interceptor
+
+import java.lang.annotation.Documented
+import java.lang.annotation.Retention
+import java.lang.annotation.Target
+
+import jakarta.enterprise.util.AnnotationLiteral
+import jakarta.interceptor.InterceptorBinding
+import org.jboss.weld.junit.MockInterceptor
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+import static jakarta.enterprise.inject.spi.InterceptionType.AROUND_INVOKE
+import static java.lang.annotation.ElementType.METHOD
+import static java.lang.annotation.ElementType.TYPE
+import static java.lang.annotation.RetentionPolicy.RUNTIME
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class MockInterceptorTest extends Specification {
+ def aroundInvokes = []
+
+ def postConstructs = []
+
+ @WeldSetup
+ def weld = WeldInitiator
+ .from(Foo)
+ .addBeans(
+ MockInterceptor
+ .withBindings(BarBinding.Literal.INSTANCE)
+ .aroundInvoke { ctx, b ->
+ aroundInvokes.add(b.beanClass.name)
+ ctx.proceed()
+ },
+ // This interceptor is disabled
+ MockInterceptor
+ .withBindings(BarBinding.Literal.INSTANCE)
+ .beanClass(String)
+ .aroundInvoke { ctx, b ->
+ false
+ },
+ MockInterceptor
+ .withBindings(FooBinding.Literal.INSTANCE)
+ .postConstruct { ctx, b ->
+ postConstructs.add(b.beanClass.name)
+ })
+ .build()
+
+ def 'interception should work properly'() {
+ expect:
+ aroundInvokes.empty
+ postConstructs.empty
+
+ when:
+ def pong = weld.select(Foo).get().ping()
+
+ then:
+ pong
+ verifyAll {
+ aroundInvokes == [Foo.name]
+ postConstructs == [Foo.name]
+ }
+ }
+
+ def 'disabled interceptor should not be resolved'() {
+ when:
+ def interceptors = weld.beanManager.resolveInterceptors(AROUND_INVOKE, BarBinding.Literal.INSTANCE)
+
+ then:
+ interceptors*.beanClass == [MockInterceptor]
+ }
+
+ @FooBinding
+ static class Foo {
+ @BarBinding
+ boolean ping() {
+ return true
+ }
+ }
+
+ @Target(TYPE)
+ @Retention(RUNTIME)
+ @Documented
+ @InterceptorBinding
+ static @interface FooBinding {
+ static final class Literal extends AnnotationLiteral implements FooBinding {
+ public static final Literal INSTANCE = new Literal()
+ }
+ }
+
+ @Target(METHOD)
+ @Retention(RUNTIME)
+ @Documented
+ @InterceptorBinding
+ static @interface BarBinding {
+ static final class Literal extends AnnotationLiteral implements BarBinding {
+ public static final Literal INSTANCE = new Literal()
+ }
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/Alpha.groovy b/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/Alpha.groovy
new file mode 100644
index 00000000..9b4bcf4c
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/Alpha.groovy
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.ofpackage
+
+import jakarta.annotation.PostConstruct
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class Alpha {
+ private String value
+
+ @PostConstruct
+ void init() {
+ value = this.getClass().simpleName.toLowerCase()
+ }
+
+ String getValue() {
+ return value
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/Bravo.groovy b/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/Bravo.groovy
new file mode 100644
index 00000000..a2885d32
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/Bravo.groovy
@@ -0,0 +1,38 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.ofpackage
+
+import jakarta.annotation.PostConstruct
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class Bravo {
+ private String value
+
+ @PostConstruct
+ void init() {
+ value = this.getClass().simpleName.toLowerCase()
+ }
+
+ String getValue() {
+ return value
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/OfPackageTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/OfPackageTest.groovy
new file mode 100644
index 00000000..2318eec0
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/ofpackage/OfPackageTest.groovy
@@ -0,0 +1,40 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.ofpackage
+
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import org.jboss.weld.spock.basic.Foo
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class OfPackageTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator.ofTestPackage()
+
+ def 'ofTestPackage should add beans in the package of the test class'() {
+ expect:
+ weld.select(Alpha).get().value == 'alpha'
+ weld.select(Bravo).resolvable
+ !weld.select(Foo).resolvable
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/resources/DummySessionBean.groovy b/spock/src/test/groovy/org/jboss/weld/spock/resources/DummySessionBean.groovy
new file mode 100644
index 00000000..4318a6f5
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/resources/DummySessionBean.groovy
@@ -0,0 +1,29 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.resources
+
+/**
+ * @author Björn Kautler
+ */
+class DummySessionBean {
+ final String id
+
+ DummySessionBean(String id) {
+ this.id = id
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/resources/FooEjbs.groovy b/spock/src/test/groovy/org/jboss/weld/spock/resources/FooEjbs.groovy
new file mode 100644
index 00000000..54ff7f1b
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/resources/FooEjbs.groovy
@@ -0,0 +1,30 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.resources
+
+import jakarta.ejb.EJB
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class FooEjbs {
+ @EJB
+ DummySessionBean dummySessionBean
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/resources/FooJpa.groovy b/spock/src/test/groovy/org/jboss/weld/spock/resources/FooJpa.groovy
new file mode 100644
index 00000000..4225a462
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/resources/FooJpa.groovy
@@ -0,0 +1,36 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.resources
+
+import jakarta.enterprise.context.Dependent
+import jakarta.persistence.EntityManager
+import jakarta.persistence.EntityManagerFactory
+import jakarta.persistence.PersistenceContext
+import jakarta.persistence.PersistenceUnit
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class FooJpa {
+ @PersistenceContext
+ EntityManager entityManager
+
+ @PersistenceUnit
+ EntityManagerFactory entityManagerFactory
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/resources/FooResources.groovy b/spock/src/test/groovy/org/jboss/weld/spock/resources/FooResources.groovy
new file mode 100644
index 00000000..6d1c4e0d
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/resources/FooResources.groovy
@@ -0,0 +1,37 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.resources
+
+import jakarta.annotation.Resource
+import jakarta.enterprise.context.Dependent
+
+/**
+ * @author Björn Kautler
+ */
+@Dependent
+class FooResources {
+ @Resource(lookup = 'bar')
+ String bar
+
+ String baz
+
+ @Resource(name = 'baz')
+ void setBaz(String baz) {
+ this.baz = baz
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/resources/InjectResourcesTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/resources/InjectResourcesTest.groovy
new file mode 100644
index 00000000..8421a9e1
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/resources/InjectResourcesTest.groovy
@@ -0,0 +1,69 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.resources
+
+import jakarta.persistence.EntityManager
+import jakarta.persistence.EntityManagerFactory
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Specification
+
+/**
+ * @author Björn Kautler
+ */
+@EnableWeld
+class InjectResourcesTest extends Specification {
+ @WeldSetup
+ def weld = WeldInitiator
+ .fromTestPackage()
+ .bindResource('bar', 'hello1')
+ .bindResource('java:comp/env/baz', 'hello2')
+ .setEjbFactory { new DummySessionBean('ping') }
+ .setPersistenceUnitFactory { Mock(EntityManagerFactory) }
+ .setPersistenceContextFactory { Mock(EntityManager) }
+ .build()
+
+ def 'resource injection should work properly'() {
+ when:
+ def foo = weld.select(FooResources).get()
+
+ then:
+ foo.bar == 'hello1'
+ foo.baz == 'hello2'
+ }
+
+ def 'EJB injection should work properly'() {
+ when:
+ def foo = weld.select(FooEjbs).get()
+
+ then:
+ foo.dummySessionBean.id == 'ping'
+ }
+
+ def 'JPA injection should work properly'() {
+ when:
+ def foo = weld.select(FooJpa).get()
+
+ then:
+ foo.entityManagerFactory != null
+ !foo.entityManagerFactory.open
+ foo.entityManager != null
+ !foo.entityManager.open
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/scope/PlainBean.groovy b/spock/src/test/groovy/org/jboss/weld/spock/scope/PlainBean.groovy
new file mode 100644
index 00000000..444e880f
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/scope/PlainBean.groovy
@@ -0,0 +1,27 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.scope
+
+import jakarta.enterprise.context.ApplicationScoped
+
+/**
+ * @author Björn Kautler
+ */
+@ApplicationScoped
+class PlainBean {
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/scope/WeldScopeTest.groovy b/spock/src/test/groovy/org/jboss/weld/spock/scope/WeldScopeTest.groovy
new file mode 100644
index 00000000..331536b9
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/scope/WeldScopeTest.groovy
@@ -0,0 +1,94 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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 org.jboss.weld.spock.scope
+
+import org.jboss.weld.environment.se.Weld
+import org.jboss.weld.environment.se.WeldContainer
+import org.jboss.weld.spock.EnableWeld
+import org.jboss.weld.spock.WeldInitiator
+import org.jboss.weld.spock.WeldSetup
+import spock.lang.Isolated
+import spock.lang.Rollup
+import spock.lang.Shared
+import spock.lang.Specification
+
+import static org.jboss.weld.spock.EnableWeld.Scope.FEATURE
+import static org.jboss.weld.spock.EnableWeld.Scope.SPECIFICATION
+
+/**
+ * @author Björn Kautler
+ */
+class WeldScopeTest extends Specification {
+ @WeldSetup
+ def initiator = WeldInitiator.of(new Weld(String.valueOf(System.nanoTime()))
+ .disableDiscovery().addBeanClass(PlainBean))
+
+ @Shared
+ def containerId
+}
+
+@Isolated
+@EnableWeld(scope = SPECIFICATION)
+class SpecificationWeldScopeTest extends WeldScopeTest {
+ @Rollup
+ def 'all iterations should use the same container with scope SPECIFICATION'() {
+ given:
+ if (containerId == null) {
+ containerId = WeldContainer.current().id
+ }
+
+ expect:
+ containerId == WeldContainer.current().id
+
+ where:
+ i << (1..2)
+ }
+}
+
+@Isolated
+class OthersWeldScopeTest extends WeldScopeTest {
+ @Rollup
+ @EnableWeld(scope = FEATURE)
+ def 'all iterations should use the same container with scope FEATURE'() {
+ given:
+ if (containerId == null) {
+ containerId = WeldContainer.current().id
+ }
+
+ expect:
+ containerId == WeldContainer.current().id
+
+ where:
+ i << (1..2)
+ }
+
+ @Rollup
+ @EnableWeld
+ def 'all iterations should use different containers with scope ITERATION'() {
+ given:
+ if (containerId == null) {
+ containerId = WeldContainer.current().id
+ }
+
+ expect:
+ containerId != WeldContainer.current().id
+
+ where:
+ i << (1..2)
+ }
+}
diff --git a/spock/src/test/groovy/org/jboss/weld/spock/util/EmbeddedSpecRunnerWrapper.groovy b/spock/src/test/groovy/org/jboss/weld/spock/util/EmbeddedSpecRunnerWrapper.groovy
new file mode 100644
index 00000000..1131c7c5
--- /dev/null
+++ b/spock/src/test/groovy/org/jboss/weld/spock/util/EmbeddedSpecRunnerWrapper.groovy
@@ -0,0 +1,30 @@
+package org.jboss.weld.spock.util
+
+import org.junit.platform.engine.DiscoverySelector
+import org.junit.platform.testkit.engine.EngineTestKit
+import spock.util.EmbeddedSpecRunner
+
+/**
+ * Work-around for a strange Groovy behavior.
+ *
+ * The embedded spec runner is used to test the exception if there is automagic mode combined with
+ * {@code @WeldSetup}. But for whatever reason, without this wrapper that just has a plain copy of the private
+ * {@code doRunRequest} method in the parent class, we get a {@code NoMethodFoundException}.
+ *
+ *
Until someone finds out where this problem comes from, this ugly hack at least works in the meantime.
+ *
+ * @author Björn Kautler
+ */
+class EmbeddedSpecRunnerWrapper extends EmbeddedSpecRunner {
+ SummarizedEngineExecutionResults doRunRequest(List selectors) {
+ def executionResults = EngineTestKit
+ .engine("spock")
+ .selectors(*selectors)
+ .execute()
+ def first = executionResults.allEvents().executions().failed().stream().findFirst()
+ if (first.present) {
+ throw first.get().terminationInfo.executionResult.throwable.get()
+ }
+ return new SummarizedEngineExecutionResults(executionResults)
+ }
+}
diff --git a/spock/src/test/resources/META-INF/services/org.jboss.weld.spock.WeldSpockEnricher b/spock/src/test/resources/META-INF/services/org.jboss.weld.spock.WeldSpockEnricher
new file mode 100644
index 00000000..0fdaa8fa
--- /dev/null
+++ b/spock/src/test/resources/META-INF/services/org.jboss.weld.spock.WeldSpockEnricher
@@ -0,0 +1 @@
+org.jboss.weld.spock.enricher.FooWeldSpockEnricher
diff --git a/spock/src/test/resources/SpockConfig.groovy b/spock/src/test/resources/SpockConfig.groovy
new file mode 100644
index 00000000..202ff490
--- /dev/null
+++ b/spock/src/test/resources/SpockConfig.groovy
@@ -0,0 +1,22 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2022, Red Hat, Inc., and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * 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.
+ */
+
+runner {
+ parallel {
+ enabled true
+ }
+}
diff --git a/spock/src/test/resources/log4j2-test.xml b/spock/src/test/resources/log4j2-test.xml
new file mode 100644
index 00000000..a982c6f0
--- /dev/null
+++ b/spock/src/test/resources/log4j2-test.xml
@@ -0,0 +1,16 @@
+
+
+
+
+
+ <%-35.35t> <%x> <%X> <%50.50c> %m}{TRACE = magenta}%n]]>
+
+
+
+
+
+
+
+
+
+