Repository: weld/weld-junit
Pull request #76 (2019-04-10 03:14:30)

Fix class scanning forcing classes as beans
Due to _some_ discovered classes bean added directly to `Weld::addBeanClass` they would be treated as CDI beans with default scopes and qualifiers; even when they were not supposed to be considered beans.

Now only classes added via the `AddBeanClasses`,  `AddEnabledInterceptors` and `AddEnabledDecorators` annotations are forcably treated as beans.  All other classes must contain a bean defining annotation (as described by spec 2.0)

kdubb: @manovotn I've added the tests and reworked annotation identification per your review.

I reordered the commits so you can see easily reset HEAD to isolate the tests and see the failures, which are then fixed by the following commit.

Patch:
diff --git a/.travis.yml b/.travis.yml
index 01cc7e7..e4bf80f 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -2,8 +2,5 @@ language: java
 jdk:
   - oraclejdk8
   - openjdk8
-env:
-  - WELD_JUNIT_PROFILE=weld2
-  - WELD_JUNIT_PROFILE=weld3
 script: "WELD_JUNIT_DEBUG=spotbugs mvn verify -Dspotbugs.failOnError=true"
 sudo: false
diff --git a/junit-common/pom.xml b/junit-common/pom.xml
index f76f23d..78bc236 100644
--- a/junit-common/pom.xml
+++ b/junit-common/pom.xml
@@ -4,7 +4,7 @@
    <parent>
       <groupId>org.jboss.weld</groupId>
       <artifactId>weld-junit-parent</artifactId>
-      <version>1.3.2-SNAPSHOT</version>
+      <version>2.0.0-SNAPSHOT</version>
    </parent>
 
    <artifactId>weld-junit-common</artifactId>
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/ContextImpl.java b/junit-common/src/main/java/org/jboss/weld/junit/ContextImpl.java
index 229d98a..dd19df7 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/ContextImpl.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/ContextImpl.java
@@ -22,6 +22,8 @@ import java.util.Map;
 import java.util.logging.Logger;
 
 import javax.enterprise.context.ContextNotActiveException;
+import javax.enterprise.context.Destroyed;
+import javax.enterprise.context.Initialized;
 import javax.enterprise.context.spi.Context;
 import javax.enterprise.context.spi.Contextual;
 import javax.enterprise.context.spi.CreationalContext;
@@ -88,7 +90,7 @@ class ContextImpl implements Context {
 
     public void activate() {
         currentContext.set(new HashMap<Contextual<?>, ContextualInstance<?>>());
-        beanManager.fireEvent(new Object(), InitializedLiteral.of(scope));
+        beanManager.fireEvent(new Object(), Initialized.Literal.of(scope));
     }
 
     public void deactivate() {
@@ -105,7 +107,7 @@ class ContextImpl implements Context {
         }
         ctx.clear();
         currentContext.remove();
-        beanManager.fireEvent(new Object(), DestroyedLiteral.of(scope));
+        beanManager.fireEvent(new Object(), Destroyed.Literal.of(scope));
     }
 
     /**
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/DestroyedLiteral.java b/junit-common/src/main/java/org/jboss/weld/junit/DestroyedLiteral.java
deleted file mode 100644
index a3fddc7..0000000
--- a/junit-common/src/main/java/org/jboss/weld/junit/DestroyedLiteral.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2018, 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.junit;
-
-import java.lang.annotation.Annotation;
-
-import javax.enterprise.context.Destroyed;
-import javax.enterprise.util.AnnotationLiteral;
-
-/**
- * Supports inline instantiation of the {@link Destroyed} qualifier.
- *
- * @author Martin Kouba
- */
-class DestroyedLiteral extends AnnotationLiteral<Destroyed> implements Destroyed {
-
-    private static final long serialVersionUID = 1L;
-
-    private final Class<? extends Annotation> value;
-
-    public static DestroyedLiteral of(Class<? extends Annotation> value) {
-        return new DestroyedLiteral(value);
-    }
-
-    private DestroyedLiteral(Class<? extends Annotation> value) {
-        this.value = value;
-    }
-
-    public Class<? extends Annotation> value() {
-        return value;
-    }
-}
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/InitializedLiteral.java b/junit-common/src/main/java/org/jboss/weld/junit/InitializedLiteral.java
deleted file mode 100644
index 7e9e7b5..0000000
--- a/junit-common/src/main/java/org/jboss/weld/junit/InitializedLiteral.java
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2018, 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.junit;
-
-import java.lang.annotation.Annotation;
-
-import javax.enterprise.context.Initialized;
-import javax.enterprise.util.AnnotationLiteral;
-
-/**
- * Supports inline instantiation of the {@link Initialized} qualifier.
- *
- * @author Martin Kouba
- */
-class InitializedLiteral extends AnnotationLiteral<Initialized> implements Initialized {
-
-    private static final long serialVersionUID = 1L;
-
-    private final Class<? extends Annotation> value;
-
-    public static InitializedLiteral of(Class<? extends Annotation> value) {
-        return new InitializedLiteral(value);
-    }
-
-    private InitializedLiteral(Class<? extends Annotation> value) {
-        this.value = value;
-    }
-
-    public Class<? extends Annotation> value() {
-        return value;
-    }
-}
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockBean.java b/junit-common/src/main/java/org/jboss/weld/junit/MockBean.java
index f46b1b3..24a0e68 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/MockBean.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/MockBean.java
@@ -36,6 +36,7 @@ import javax.enterprise.inject.Stereotype;
 import javax.enterprise.inject.spi.Bean;
 import javax.enterprise.inject.spi.InjectionPoint;
 import javax.enterprise.inject.spi.PassivationCapable;
+import javax.enterprise.inject.spi.Prioritized;
 import javax.enterprise.inject.spi.Unmanaged;
 import javax.enterprise.inject.spi.Unmanaged.UnmanagedInstance;
 import javax.enterprise.util.AnnotationLiteral;
@@ -136,7 +137,7 @@ public class MockBean<T> implements Bean<T>, PassivationCapable {
 
     private final Class<?> beanClass;
 
-    private MockBean(Class<?> beanClass, Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean selectForSyntheticBeanArchive, String name,
+    protected MockBean(Class<?> beanClass, Set<Class<? extends Annotation>> stereotypes, boolean alternative, boolean selectForSyntheticBeanArchive, String name,
             Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope, CreateFunction<T> createCallback,
             DestroyFunction<T> destroyCallback) {
         this.beanClass = beanClass;
@@ -325,6 +326,8 @@ public class MockBean<T> implements Bean<T>, PassivationCapable {
 
         private boolean selectForSyntheticBeanArchive;
 
+        private Integer priority;
+
         private String name;
 
         private Set<Annotation> qualifiers;
@@ -346,6 +349,7 @@ public class MockBean<T> implements Bean<T>, PassivationCapable {
             this.types = new HashSet<>();
             this.types.add(Object.class);
             this.beanClass = WeldCDIExtension.class;
+            this.priority = null;
         }
 
         /**
@@ -447,6 +451,32 @@ public class MockBean<T> implements Bean<T>, PassivationCapable {
             return this;
         }
 
+        /**
+         * Programmatic equivalent to to putting {@link javax.annotation.Priority} annotation on a bean class.
+         * Allows for globally enabled alternatives.
+         *
+         * @param priority
+         * @return self
+         * @see Prioritized#getPriority()
+         */
+        public Builder<T> priority(int priority) {
+            this.priority = priority;
+            return this;
+        }
+
+        /**
+         * This beans is a globally enabled alternative with a priority equal to its method parameter.
+         * Calling this method is a shortcut for {@link Builder#priority(int)} and {@link Builder#alternative(boolean)}
+         *
+         * @param priority
+         * @return self
+         */
+        public Builder<T> globallySelectedAlternative(int priority) {
+            this.priority = priority;
+            this.alternative = true;
+            return this;
+        }
+
         /**
          * The bean is an alternative and should be automatically selected for the synthetic bean archive.
          *
@@ -455,7 +485,6 @@ public class MockBean<T> implements Bean<T>, PassivationCapable {
          * selected for a bean archive. By default, all mock beans share the same bean class - {@code org.jboss.weld.junit.WeldCDIExtension}.
          * </p>
          *
-         * @param value
          * @return self
          * @see Bean#isAlternative()
          * @see Weld#addAlternative(Class)
@@ -496,7 +525,7 @@ public class MockBean<T> implements Bean<T>, PassivationCapable {
 
         /**
          *
-         * @param qualifier
+         * @param stereotype
          * @return self
          */
         public Builder<T> addStereotype(Class<? extends Annotation> stereotype) {
@@ -594,8 +623,14 @@ public class MockBean<T> implements Bean<T>, PassivationCapable {
                     }
                 }
             }
-            return new MockBean<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name, qualifiers, types, scope, createCallback,
-                    destroyCallback);
+            // if given any priority, we will instead initialize MockBeanWithPriority
+            if (priority != null) {
+                return new MockBeanWithPriority<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, priority, name, qualifiers, types, scope, createCallback,
+                        destroyCallback);
+            } else {
+                return new MockBean<>(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name, qualifiers, types, scope, createCallback,
+                        destroyCallback);
+            }
         }
 
     }
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockBeanWithPriority.java b/junit-common/src/main/java/org/jboss/weld/junit/MockBeanWithPriority.java
new file mode 100644
index 0000000..6aca6ff
--- /dev/null
+++ b/junit-common/src/main/java/org/jboss/weld/junit/MockBeanWithPriority.java
@@ -0,0 +1,33 @@
+package org.jboss.weld.junit;
+
+import javax.enterprise.inject.spi.Prioritized;
+import java.lang.annotation.Annotation;
+import java.lang.reflect.Type;
+import java.util.Set;
+
+/**
+ * A subclass of {@link MockBean} implementing {@link Prioritized} hence allowing for globally enabled alternatives.
+ * Used instead of {@link MockBean} if user specified {@link MockBean.Builder#priority(int)}.
+ *
+ * @author Matej Novotny
+ */
+class MockBeanWithPriority<T> extends MockBean<T> implements Prioritized {
+
+    private final int priority;
+
+    MockBeanWithPriority(Class<?> beanClass, Set<Class<? extends Annotation>> stereotypes, boolean alternative,
+                                 boolean selectForSyntheticBeanArchive, int priority, String name,
+                                 Set<Annotation> qualifiers, Set<Type> types, Class<? extends Annotation> scope,
+                                 CreateFunction<T> createCallback, DestroyFunction<T> destroyCallback) {
+        super(beanClass, stereotypes, alternative, selectForSyntheticBeanArchive, name, qualifiers, types, scope, createCallback, destroyCallback);
+        if (priority <= 0) {
+            throw new IllegalArgumentException("MockBean cannot have priority equal or lower than 0!");
+        }
+        this.priority = priority;
+    }
+
+    @Override
+    public int getPriority() {
+        return this.priority;
+    }
+}
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockEjbInjectionServices.java b/junit-common/src/main/java/org/jboss/weld/junit/MockEjbInjectionServices.java
index be0027b..4a36e8f 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/MockEjbInjectionServices.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/MockEjbInjectionServices.java
@@ -48,14 +48,9 @@ public class MockEjbInjectionServices implements EjbInjectionServices {
         return new ResourceReferenceFactory<Object>() {
             @Override
             public ResourceReference<Object> createResource() {
-                return new SimpleResourceReference<Object>(resolveEjb(injectionPoint));
+                return new SimpleResourceReference<Object>(ejbFactory.apply(injectionPoint));
             }
         };
     }
 
-    // only for Weld 2
-    public Object resolveEjb(InjectionPoint injectionPoint) {
-        return ejbFactory.apply(injectionPoint);
-    }
-
 }
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockJpaInjectionServices.java b/junit-common/src/main/java/org/jboss/weld/junit/MockJpaInjectionServices.java
index 2167916..6a78dc1 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/MockJpaInjectionServices.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/MockJpaInjectionServices.java
@@ -49,7 +49,14 @@ public class MockJpaInjectionServices implements JpaInjectionServices {
         return new ResourceReferenceFactory<EntityManager>() {
             @Override
             public ResourceReference<EntityManager> createResource() {
-                return new SimpleResourceReference<EntityManager>(resolvePersistenceContext(injectionPoint));
+                if (persistenceContextFactory == null) {
+                    throw new IllegalStateException("Persistent context factory not set, cannot resolve injection point: " + injectionPoint);
+                }
+                Object context = persistenceContextFactory.apply(injectionPoint);
+                if (context == null || context instanceof EntityManager) {
+                    return new SimpleResourceReference<EntityManager>((EntityManager) context);
+                }
+                throw new IllegalStateException("Not an EntityManager instance: " + context);
             }
         };
     }
@@ -59,35 +66,18 @@ public class MockJpaInjectionServices implements JpaInjectionServices {
         return new ResourceReferenceFactory<EntityManagerFactory>() {
             @Override
             public ResourceReference<EntityManagerFactory> createResource() {
-                return new SimpleResourceReference<EntityManagerFactory>(resolvePersistenceUnit(injectionPoint));
+                if (persistenceUnitFactory == null) {
+                    throw new IllegalStateException("Persistent unit factory not set, cannot resolve injection point: " + injectionPoint);
+                }
+                Object unit = persistenceUnitFactory.apply(injectionPoint);
+                if (unit == null || unit instanceof EntityManagerFactory) {
+                    return new SimpleResourceReference<EntityManagerFactory>((EntityManagerFactory) unit);
+                }
+                throw new IllegalStateException("Not an EntityManagerFactory instance: " + unit);
             }
         };
     }
 
-    // only for Weld 2
-    public EntityManager resolvePersistenceContext(InjectionPoint injectionPoint) {
-        if (persistenceContextFactory == null) {
-            throw new IllegalStateException("Persistent context factory not set, cannot resolve injection point: " + injectionPoint);
-        }
-        Object context = persistenceContextFactory.apply(injectionPoint);
-        if (context == null || context instanceof EntityManager) {
-            return (EntityManager) context;
-        }
-        throw new IllegalStateException("Not an EntityManager instance: " + context);
-    }
-
-    // only for Weld 2
-    public EntityManagerFactory resolvePersistenceUnit(InjectionPoint injectionPoint) {
-        if (persistenceUnitFactory == null) {
-            throw new IllegalStateException("Persistent unit factory not set, cannot resolve injection point: " + injectionPoint);
-        }
-        Object unit = persistenceUnitFactory.apply(injectionPoint);
-        if (unit == null || unit instanceof EntityManagerFactory) {
-            return (EntityManagerFactory) unit;
-        }
-        throw new IllegalStateException("Not an EntityManagerFactory instance: " + unit);
-    }
-
     @Override
     public void cleanup() {
     }
diff --git a/junit-common/src/main/java/org/jboss/weld/junit/MockResourceInjectionServices.java b/junit-common/src/main/java/org/jboss/weld/junit/MockResourceInjectionServices.java
index 2b28456..ecbe444 100644
--- a/junit-common/src/main/java/org/jboss/weld/junit/MockResourceInjectionServices.java
+++ b/junit-common/src/main/java/org/jboss/weld/junit/MockResourceInjectionServices.java
@@ -49,8 +49,7 @@ public class MockResourceInjectionServices implements ResourceInjectionServices
         this.resources = ImmutableMap.copyOf(resources);
     }
 
-    // only for Weld 2
-    public Object resolveResource(InjectionPoint injectionPoint) {
+    private Object resolveResource(InjectionPoint injectionPoint) {
         Resource resource = getResourceAnnotation(injectionPoint);
         if (resource == null) {
             throw new IllegalArgumentException("No @Resource annotation found on " + injectionPoint);
@@ -83,11 +82,6 @@ public class MockResourceInjectionServices implements ResourceInjectionServices
         throw new UnsupportedOperationException();
     }
 
-    // only for Weld 2
-    public Object resolveResource(String jndiName, String mappedName) {
-        throw new UnsupportedOperationException();
-    }
-
     @Override
     public void cleanup() {
     }
diff --git a/junit4/pom.xml b/junit4/pom.xml
index b584bea..370c8e4 100644
--- a/junit4/pom.xml
+++ b/junit4/pom.xml
@@ -4,7 +4,7 @@
    <parent>
       <groupId>org.jboss.weld</groupId>
       <artifactId>weld-junit-parent</artifactId>
-      <version>1.3.2-SNAPSHOT</version>
+      <version>2.0.0-SNAPSHOT</version>
    </parent>
 
    <artifactId>weld-junit4</artifactId>
@@ -43,29 +43,13 @@
          <scope>test</scope>
       </dependency>
 
-   </dependencies>
-
-   <profiles>
-
-      <profile>
-         <id>weld3</id>
-         <activation>
-            <activeByDefault>false</activeByDefault>
-            <property>
-               <name>env.WELD_JUNIT_PROFILE</name>
-               <value>weld3</value>
-            </property>
-         </activation>
-         <dependencies>
-            <dependency>
-               <groupId>org.jboss.weld.module</groupId>
-               <artifactId>weld-ejb</artifactId>
-               <version>${version.weld}</version>
-               <scope>test</scope>
-            </dependency>
-         </dependencies>
-      </profile>
+      <dependency>
+         <groupId>org.jboss.weld.module</groupId>
+         <artifactId>weld-ejb</artifactId>
+         <version>${version.weld}</version>
+         <scope>test</scope>
+      </dependency>
 
-   </profiles>
+   </dependencies>
 
 </project>
diff --git a/junit5/pom.xml b/junit5/pom.xml
index 05454c8..bcda77c 100644
--- a/junit5/pom.xml
+++ b/junit5/pom.xml
@@ -5,7 +5,7 @@
    <parent>
       <groupId>org.jboss.weld</groupId>
       <artifactId>weld-junit-parent</artifactId>
-      <version>1.3.2-SNAPSHOT</version>
+      <version>2.0.0-SNAPSHOT</version>
    </parent>
 
    <artifactId>weld-junit5</artifactId>
@@ -48,30 +48,14 @@
          <artifactId>hibernate-jpa-2.1-api</artifactId>
          <scope>test</scope>
       </dependency>
+
+      <dependency>
+         <groupId>org.jboss.weld.module</groupId>
+         <artifactId>weld-ejb</artifactId>
+         <version>${version.weld}</version>
+         <scope>test</scope>
+      </dependency>
       
    </dependencies>
 
-   <profiles>
-
-      <profile>
-         <id>weld3</id>
-         <activation>
-            <activeByDefault>false</activeByDefault>
-            <property>
-               <name>env.WELD_JUNIT_PROFILE</name>
-               <value>weld3</value>
-            </property>
-         </activation>
-         <dependencies>
-            <dependency>
-               <groupId>org.jboss.weld.module</groupId>
-               <artifactId>weld-ejb</artifactId>
-               <version>${version.weld}</version>
-               <scope>test</scope>
-            </dependency>
-         </dependencies>
-      </profile>
-
-   </profiles>
-
 </project>
diff --git a/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java b/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java
index 815019f..4e38768 100644
--- a/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java
+++ b/junit5/src/main/java/org/jboss/weld/junit5/auto/ClassScanning.java
@@ -25,21 +25,18 @@ import org.junit.jupiter.api.RepeatedTest;
 import org.junit.jupiter.api.Test;
 import org.junit.platform.commons.support.AnnotationSupport;
 import org.junit.platform.commons.support.HierarchyTraversalMode;
-import org.junit.platform.commons.util.AnnotationUtils;
 import org.junit.platform.commons.util.CollectionUtils;
 import org.junit.platform.commons.util.Preconditions;
 
-import javax.enterprise.context.ApplicationScoped;
-import javax.enterprise.context.ConversationScoped;
+import javax.decorator.Decorator;
 import javax.enterprise.context.Dependent;
 import javax.enterprise.context.NormalScope;
-import javax.enterprise.context.RequestScoped;
-import javax.enterprise.context.SessionScoped;
 import javax.enterprise.inject.Produces;
 import javax.enterprise.inject.Stereotype;
 import javax.enterprise.inject.spi.Extension;
 import javax.inject.Inject;
 import javax.inject.Qualifier;
+import javax.interceptor.Interceptor;
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Constructor;
 import java.lang.reflect.Executable;
@@ -107,7 +104,7 @@ class ClassScanning {
 
             findFirstAnnotatedConstructor(currClass, Inject.class)
                     .map(Stream::of).orElseGet(Stream::empty)
-                    .flatMap(cons -> getExecutableParameterTypes(cons, weld, explicitInjection).stream())
+                    .flatMap(cons -> getExecutableParameterTypes(cons, explicitInjection).stream())
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             findAnnotatedFields(currClass, Produces.class).stream()
@@ -115,31 +112,36 @@ class ClassScanning {
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findAnnotatedMethods(currClass, Produces.class, HierarchyTraversalMode.BOTTOM_UP).stream()
-                    .map(Method::getReturnType)
+                    .flatMap(method ->
+                        Stream.concat(
+                                getExecutableParameterTypes(method, explicitInjection).stream(),
+                                Stream.of(method.getReturnType())
+                        )
+                    )
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findAnnotatedMethods(currClass, Test.class, HierarchyTraversalMode.BOTTOM_UP).stream()
-                    .flatMap(method -> getExecutableParameterTypes(method, weld, explicitInjection).stream())
+                    .flatMap(method -> getExecutableParameterTypes(method, explicitInjection).stream())
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findAnnotatedMethods(currClass, RepeatedTest.class, HierarchyTraversalMode.BOTTOM_UP).stream()
-                    .flatMap(method -> getExecutableParameterTypes(method, weld, explicitInjection).stream())
+                    .flatMap(method -> getExecutableParameterTypes(method, explicitInjection).stream())
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findAnnotatedMethods(currClass, BeforeAll.class, HierarchyTraversalMode.BOTTOM_UP).stream()
-                    .flatMap(method -> getExecutableParameterTypes(method, weld, explicitInjection).stream())
+                    .flatMap(method -> getExecutableParameterTypes(method, explicitInjection).stream())
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findAnnotatedMethods(currClass, BeforeEach.class, HierarchyTraversalMode.BOTTOM_UP).stream()
-                    .flatMap(method -> getExecutableParameterTypes(method, weld, explicitInjection).stream())
+                    .flatMap(method -> getExecutableParameterTypes(method, explicitInjection).stream())
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findAnnotatedMethods(currClass, AfterEach.class, HierarchyTraversalMode.BOTTOM_UP).stream()
-                    .flatMap(method -> getExecutableParameterTypes(method, weld, explicitInjection).stream())
+                    .flatMap(method -> getExecutableParameterTypes(method, explicitInjection).stream())
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findAnnotatedMethods(currClass, AfterAll.class, HierarchyTraversalMode.BOTTOM_UP).stream()
-                    .flatMap(method -> getExecutableParameterTypes(method, weld, explicitInjection).stream())
+                    .flatMap(method -> getExecutableParameterTypes(method, explicitInjection).stream())
                     .forEach(cls -> addClassesToProcess(classesToProcess, cls));
 
             AnnotationSupport.findRepeatableAnnotations(currClass, AddPackages.class)
@@ -151,7 +153,10 @@ class ClassScanning {
             AnnotationSupport.findRepeatableAnnotations(currClass, AddBeanClasses.class).stream()
                     .flatMap(ann -> stream(ann.value()))
                     .distinct()
-                    .forEach(weld::addBeanClass);
+                    .forEach(it -> {
+                        classesToProcess.add(it);
+                        weld.addBeanClass(it);
+                    });
 
             AnnotationSupport.findRepeatableAnnotations(currClass, AddExtensions.class).stream()
                     .flatMap(ann -> stream(ann.value()))
@@ -163,6 +168,7 @@ class ClassScanning {
                     .flatMap(ann -> stream(ann.value()))
                     .distinct()
                     .forEach(interceptor -> {
+                        classesToProcess.add(interceptor);
                         weld.addInterceptor(interceptor);
                         weld.addBeanClass(interceptor);
                     });
@@ -171,6 +177,7 @@ class ClassScanning {
                     .flatMap(ann -> stream(ann.value()))
                     .distinct()
                     .forEach(decorator -> {
+                        classesToProcess.add(decorator);
                         weld.addDecorator(decorator);
                         weld.addBeanClass(decorator);
                     });
@@ -224,24 +231,20 @@ class ClassScanning {
 
     }
 
-    private static List<Class<?>> getExecutableParameterTypes(Executable executable, Weld weld, boolean explicitInjection) {
+    private static List<Class<?>> getExecutableParameterTypes(Executable executable, boolean explicitInjection) {
 
         List<Class<?>> types = new ArrayList<>();
 
         if (explicitInjection) {
             Annotation[][] paramAnns = executable.getParameterAnnotations();
             Class<?>[] paramTypes = executable.getParameterTypes();
-            for (int c = 0; c < paramAnns.length; ++c) {
-                if (stream(paramAnns[c]).anyMatch(ann -> isAnnotated(ann.annotationType(), Qualifier.class) || isAnnotated(ann.annotationType(), NormalScope.class))) {
-                    weld.addBeanClass(paramTypes[c]);
+            for (int c = 0; c < paramTypes.length; ++c) {
+                if (stream(paramAnns[c]).anyMatch(ClassScanning::isBeanParameterAnnotation)) {
                     types.add(paramTypes[c]);
                 }
             }
         } else {
-            for (Class<?> paramType : executable.getParameterTypes()) {
-                weld.addBeanClass(paramType);
-                types.add(paramType);
-            }
+            types.addAll(asList(executable.getParameterTypes()));
         }
 
         return types;
@@ -255,14 +258,14 @@ class ClassScanning {
         }
     }
 
+    private static boolean isBeanParameterAnnotation(Annotation ann) {
+        return isAnnotated(ann.annotationType(), Qualifier.class);
+    }
+
     private static boolean hasBeanDefiningAnnotation(Class<?> clazz) {
-        return
-                AnnotationUtils.isAnnotated(clazz, ApplicationScoped.class) ||
-                        AnnotationUtils.isAnnotated(clazz, SessionScoped.class) ||
-                        AnnotationUtils.isAnnotated(clazz, ConversationScoped.class) ||
-                        AnnotationUtils.isAnnotated(clazz, RequestScoped.class) ||
-                        AnnotationUtils.isAnnotated(clazz, Dependent.class) ||
-                        AnnotationUtils.isAnnotated(clazz, Stereotype.class);
+        return isAnnotated(clazz, NormalScope.class) || isAnnotated(clazz, Dependent.class) ||
+                isAnnotated(clazz, Interceptor.class) || isAnnotated(clazz, Decorator.class) ||
+                isAnnotated(clazz, Stereotype.class);
     }
 
     private static List<Field> findAllFieldsInHierarchy(Class<?> clazz) {
diff --git a/pom.xml b/pom.xml
index 770182c..a94d891 100644
--- a/pom.xml
+++ b/pom.xml
@@ -2,7 +2,7 @@
    <modelVersion>4.0.0</modelVersion>
    <artifactId>weld-junit-parent</artifactId>
    <packaging>pom</packaging>
-   <version>1.3.2-SNAPSHOT</version>
+   <version>2.0.0-SNAPSHOT</version>
 
    <name>Weld JUnit Extensions</name>
    <description>Set of JUnit extensions to enhance the testing of CDI components.</description>
@@ -42,8 +42,7 @@
       <!-- Versions -->
       <version.junit4>4.12</version.junit4>
       <version.junit.jupiter>5.4.1</version.junit.jupiter>
-      <version.weld>2.4.8.Final</version.weld>
-      <version.weld3>3.1.0.Final</version.weld3>
+      <version.weld>3.1.0.Final</version.weld>
       <version.mockito>2.25.1</version.mockito>
       <version.jboss-ejb-api>1.0.0.Final</version.jboss-ejb-api>
       <version.hibernate-jpa-api>1.0.0.Final</version.hibernate-jpa-api>
@@ -126,19 +125,6 @@
    </dependencyManagement>
 
    <profiles>
-       <profile>
-           <id>weld3</id>
-           <activation>
-               <activeByDefault>false</activeByDefault>
-               <property>
-                   <name>env.WELD_JUNIT_PROFILE</name>
-                   <value>weld3</value>
-               </property>
-           </activation>
-           <properties>
-               <version.weld>${version.weld3}</version.weld>
-           </properties>
-       </profile>
        <profile>
            <id>spotbugs</id>
            <activation>


Test patch:
diff --git a/junit4/src/test/java/org/jboss/weld/junit4/bean/AddGloballyEnabledAlternativeTest.java b/junit4/src/test/java/org/jboss/weld/junit4/bean/AddGloballyEnabledAlternativeTest.java
new file mode 100644
index 0000000..6ad149a
--- /dev/null
+++ b/junit4/src/test/java/org/jboss/weld/junit4/bean/AddGloballyEnabledAlternativeTest.java
@@ -0,0 +1,63 @@
+package org.jboss.weld.junit4.bean;
+
+import org.jboss.weld.junit.MockBean;
+import org.jboss.weld.junit4.WeldInitiator;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mockito;
+
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.util.TypeLiteral;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Tests {@link org.jboss.weld.junit.MockBean} adding a bean that is a globally enabled alternative.
+ *
+ * @author Matej Novotny
+ */
+public class AddGloballyEnabledAlternativeTest {
+    
+    @Rule
+    public WeldInitiator weld = WeldInitiator.from(Bar.class)
+            .addBeans(createFooAlternativeBean(), createListBean())
+            .build();
+
+    public static Bean<?> createFooAlternativeBean() {
+        return MockBean.read(Foo.class)
+                .priority(3)
+                .alternative(true)
+                .addQualifier(Meaty.Literal.INSTANCE)
+                .build();
+    }
+
+    static Bean<?> createListBean() {
+        return MockBean.builder()
+                .types(new TypeLiteral<List<String>>() {
+                }.getType())
+                .globallySelectedAlternative(2)
+                .creating(
+                        // Mock object provided by Mockito
+                        Mockito.when(Mockito.mock(List.class).get(0)).thenReturn("42").getMock())
+                .build();
+    }
+
+    @Test
+    public void testAllBeansAreAddedAndCanBeSelected() {
+        Bar bar = weld.select(Bar.class).get();
+        Assert.assertNotNull(bar.getFoo());
+        Assert.assertNotNull(bar.getSomeList());
+        Assert.assertEquals("42", bar.getSomeList().get(0));
+
+        // assert all of these are actually alternatives (enabled)
+        Set<Bean<?>> beans = weld.getBeanManager().getBeans(Foo.class, Meaty.Literal.INSTANCE);
+        Assert.assertEquals(1, beans.size());
+        Assert.assertTrue(beans.iterator().next().isAlternative());
+
+        beans = weld.getBeanManager().getBeans(new TypeLiteral<List<String>>() {
+        }.getType());
+        Assert.assertEquals(1, beans.size());
+        Assert.assertTrue(beans.iterator().next().isAlternative());
+    }
+}
diff --git a/junit4/src/test/java/org/jboss/weld/junit4/bean/Bar.java b/junit4/src/test/java/org/jboss/weld/junit4/bean/Bar.java
new file mode 100644
index 0000000..84ba4b6
--- /dev/null
+++ b/junit4/src/test/java/org/jboss/weld/junit4/bean/Bar.java
@@ -0,0 +1,23 @@
+package org.jboss.weld.junit4.bean;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import java.util.List;
+
+@ApplicationScoped
+public class Bar {
+    @Inject
+    @Meaty
+    private Foo foo;
+
+    @Inject
+    private List<String> someList;
+
+    public List<String> getSomeList() {
+        return someList;
+    }
+
+    public Foo getFoo() {
+        return foo;
+    }
+}
diff --git a/junit4/src/test/java/org/jboss/weld/junit4/bean/Foo.java b/junit4/src/test/java/org/jboss/weld/junit4/bean/Foo.java
new file mode 100644
index 0000000..436638c
--- /dev/null
+++ b/junit4/src/test/java/org/jboss/weld/junit4/bean/Foo.java
@@ -0,0 +1,7 @@
+package org.jboss.weld.junit4.bean;
+
+public class Foo {
+    public String ping() {
+        return "foo";
+    }
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/ProducerMethodParametersScanningTest.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/ProducerMethodParametersScanningTest.java
new file mode 100644
index 0000000..2dd5fbc
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/ProducerMethodParametersScanningTest.java
@@ -0,0 +1,38 @@
+package org.jboss.weld.junit5.auto;
+
+import org.jboss.weld.junit5.auto.beans.Engine;
+import org.jboss.weld.junit5.auto.beans.V6;
+import org.junit.jupiter.api.Test;
+
+import javax.enterprise.context.Dependent;
+import javax.enterprise.inject.Produces;
+import javax.inject.Named;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+@EnableAutoWeld
+public class ProducerMethodParametersScanningTest {
+
+    @Produces
+    @Named("custom")
+    @Dependent
+    Engine getEngine(V6 v6) {
+        return new Engine() {
+            @Override
+            public int getThrottle() {
+                return v6.getThrottle();
+            }
+
+            @Override
+            public void setThrottle(int value) {
+                v6.setThrottle(value);
+            }
+        };
+    }
+
+    @Test
+    public void test(@Named("custom") Engine engine) {
+        assertNotNull(engine);
+    }
+
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/ScannedClassesAreNotForcedBeansTest.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/ScannedClassesAreNotForcedBeansTest.java
new file mode 100644
index 0000000..f9ab248
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/ScannedClassesAreNotForcedBeansTest.java
@@ -0,0 +1,39 @@
+package org.jboss.weld.junit5.auto;
+
+
+import org.jboss.weld.junit5.auto.beans.InjectedV8;
+import org.jboss.weld.junit5.auto.beans.V8;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import javax.enterprise.inject.Produces;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+
+@EnableAutoWeld
+@AddBeanClasses(InjectedV8.class)
+public class ScannedClassesAreNotForcedBeansTest {
+
+    /**
+     * 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.
+     * <p>
+     * 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 V8 engine = new V8();
+
+    @Test
+    @DisplayName("Test that V8 is not ambiguous to do incorrectly being identified as a bean class")
+    void test(V8 engine) {
+        assertNotNull(engine);
+    }
+
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/ScannedParameterClassesAreNotForcedBeansTest.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/ScannedParameterClassesAreNotForcedBeansTest.java
new file mode 100644
index 0000000..834ca6e
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/ScannedParameterClassesAreNotForcedBeansTest.java
@@ -0,0 +1,40 @@
+package org.jboss.weld.junit5.auto;
+
+
+import org.jboss.weld.junit5.auto.beans.ConstructedV8;
+import org.jboss.weld.junit5.auto.beans.V8;
+import org.junit.jupiter.api.DisplayName;
+import org.junit.jupiter.api.Test;
+
+import javax.enterprise.inject.Produces;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+
+
+@EnableAutoWeld
+@AddBeanClasses(ConstructedV8.class)
+public class ScannedParameterClassesAreNotForcedBeansTest {
+
+    /**
+     * 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.
+     * <p>
+     * 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 from parameters (e.g. constructor injection
+     * parameters)
+     */
+
+    @Produces
+    private V8 engine = new V8();
+
+    @Test
+    @DisplayName("Test that V8 is not ambiguous to do incorrectly being identified as a bean class from parameter")
+    void test(V8 engine) {
+        assertNotNull(engine);
+    }
+
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/beans/ConstructedV8.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/beans/ConstructedV8.java
new file mode 100644
index 0000000..a998435
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/beans/ConstructedV8.java
@@ -0,0 +1,16 @@
+package org.jboss.weld.junit5.auto.beans;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+
+@ApplicationScoped
+public class ConstructedV8 {
+
+    private V8 engine;
+
+    @Inject
+    public ConstructedV8(V8 engine) {
+        this.engine = engine;
+    }
+
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/auto/beans/InjectedV8.java b/junit5/src/test/java/org/jboss/weld/junit5/auto/beans/InjectedV8.java
new file mode 100644
index 0000000..6bf4c18
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/auto/beans/InjectedV8.java
@@ -0,0 +1,12 @@
+package org.jboss.weld.junit5.auto.beans;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+
+@ApplicationScoped
+public class InjectedV8 {
+
+    @Inject
+    private V8 engine;
+
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/bean/AddGloballyEnabledAlternativeTest.java b/junit5/src/test/java/org/jboss/weld/junit5/bean/AddGloballyEnabledAlternativeTest.java
new file mode 100644
index 0000000..4435206
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/bean/AddGloballyEnabledAlternativeTest.java
@@ -0,0 +1,66 @@
+package org.jboss.weld.junit5.bean;
+
+import org.jboss.weld.junit.MockBean;
+import org.jboss.weld.junit5.WeldInitiator;
+import org.jboss.weld.junit5.WeldJunit5Extension;
+import org.jboss.weld.junit5.WeldSetup;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mockito;
+
+import javax.enterprise.inject.spi.Bean;
+import javax.enterprise.util.TypeLiteral;
+
+import java.util.List;
+import java.util.Set;
+
+/**
+ * Tests {@link org.jboss.weld.junit.MockBean} adding a bean that is a globally enabled alternative.
+ *
+ * @author Matej Novotny
+ */
+@ExtendWith(WeldJunit5Extension.class)
+public class AddGloballyEnabledAlternativeTest {
+
+    @WeldSetup
+    public WeldInitiator weld = WeldInitiator.from(Bar.class)
+            .addBeans(createFooAlternativeBean(), createListBean())
+            .build();
+
+    public static Bean<?> createFooAlternativeBean(){
+        return MockBean.read(Foo.class)
+                .priority(3)
+                .alternative(true)
+                .addQualifier(Meaty.Literal.INSTANCE)
+                .build();
+    }
+
+    static Bean<?> createListBean() {
+        return MockBean.builder()
+                .types(new TypeLiteral<List<String>>() {
+                }.getType())
+                .globallySelectedAlternative(2)
+                .creating(
+                        // Mock object provided by Mockito
+                        Mockito.when(Mockito.mock(List.class).get(0)).thenReturn("42").getMock())
+                .build();
+    }
+
+    @Test
+    public void testAllBeansAreAddedAndCanBeSelected() {
+        Bar bar = weld.select(Bar.class).get();
+        Assertions.assertNotNull(bar.getFoo());
+        Assertions.assertNotNull(bar.getSomeList());
+        Assertions.assertEquals("42", bar.getSomeList().get(0));
+
+        // assert all of these are actually alternatives (enabled)
+        Set<Bean<?>> beans = weld.getBeanManager().getBeans(Foo.class, Meaty.Literal.INSTANCE);
+        Assertions.assertEquals(1, beans.size());
+        Assertions.assertTrue(beans.iterator().next().isAlternative());
+
+        beans = weld.getBeanManager().getBeans(new TypeLiteral<List<String>>(){}.getType());
+        Assertions.assertEquals(1, beans.size());
+        Assertions.assertTrue(beans.iterator().next().isAlternative());
+    }
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/bean/Bar.java b/junit5/src/test/java/org/jboss/weld/junit5/bean/Bar.java
new file mode 100644
index 0000000..b3b7e9a
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/bean/Bar.java
@@ -0,0 +1,24 @@
+package org.jboss.weld.junit5.bean;
+
+import javax.enterprise.context.ApplicationScoped;
+import javax.inject.Inject;
+import java.util.List;
+
+@ApplicationScoped
+public class Bar {
+
+    @Inject
+    @Meaty
+    private Foo foo;
+
+    @Inject
+    private List<String> someList;
+
+    public List<String> getSomeList() {
+        return someList;
+    }
+
+    public Foo getFoo() {
+        return foo;
+    }
+}
diff --git a/junit5/src/test/java/org/jboss/weld/junit5/bean/Foo.java b/junit5/src/test/java/org/jboss/weld/junit5/bean/Foo.java
new file mode 100644
index 0000000..2032697
--- /dev/null
+++ b/junit5/src/test/java/org/jboss/weld/junit5/bean/Foo.java
@@ -0,0 +1,8 @@
+package org.jboss.weld.junit5.bean;
+
+public class Foo {
+
+    public String ping() {
+        return "foo";
+    }
+}