Skip to content

cucumber-cdi2 ClassCastException on unmanaged instances #2242

@dcendents

Description

@dcendents

Describe the bug
cucumber-cdi2 ClassCastException on unmanaged instances

To Reproduce
Steps to reproduce the behavior:

  1. Edit the file cucumber-jvm/cdi2/pom.xml
  2. Remove the 2 dependencies for OpenWebBeans
  3. Add a dependency to Weld 3.1.6.Final
  4. Edit the file src/test/resources/META-INF/beans.xml to ignore the class CdiBellyStepDefinitions (to work around bug cucumber-cdi2 does not work with weld #2241)
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
	bean-discovery-mode="all" version="2.0">
	<scan>
		<exclude
			name="io.cucumber.cdi2.CdiBellyStepDefinitions" />
	</scan>
</beans>
  1. Edit the file src/test/resources/io/cucumber/cdi2/cukes.feature and duplicate the given step to the first scenario
  Scenario: Eat some cukes
    Given I have 4 cukes in my belly
    And I have 4 cukes in my belly
    Then there are 4 cukes in my belly
  1. Build the project
  2. Get the error
[ERROR] Eat some cukes  Time elapsed: 0.121 s  <<< ERROR!
java.lang.ClassCastException: Cannot cast javax.enterprise.inject.spi.Unmanaged$UnmanagedInstance to io.cucumber.cdi2.BellyStepDefinitions

Expected behavior
CDI to load the Step class correctly.

Your Environment

  • Versions used: 6.10.0 (but also main git branch)

Additional context

When an instance is unsatisfied, it is created manually (Unmanaged.UnmanagedInstance value).
The UnmanagedInstance wrapper is then added to standaloneInstances.
The second time, the Unmanaged.UnmanagedInstance object is retrived from the map and directly cast into the given type and fails.
I suppose a call to get() is missing (return type.cast(instance.get());)

    @Override
    public <T> T getInstance(final Class<T> type) {
        final Object instance = standaloneInstances.get(type);
        if (instance != null) {
            return type.cast(instance);
        }
        final Instance<T> selected = container.select(type);
        if (selected.isUnsatisfied()) {
            BeanManager beanManager = container.getBeanManager();
            Unmanaged<T> unmanaged = new Unmanaged<>(beanManager, type);
            Unmanaged.UnmanagedInstance<T> value = unmanaged.newInstance();
            value.produce();
            value.inject();
            value.postConstruct();
            standaloneInstances.put(type, value);
            return value.get();
        }
        return selected.get();
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions