Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cucumber-cdi2 ClassCastException on unmanaged instances #2242

Closed
dcendents opened this issue Feb 25, 2021 · 1 comment · Fixed by #2244 or #2250
Closed

cucumber-cdi2 ClassCastException on unmanaged instances #2242

dcendents opened this issue Feb 25, 2021 · 1 comment · Fixed by #2244 or #2250

Comments

@dcendents
Copy link
Contributor

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();
    }
@dcendents dcendents changed the title cucumber-cdi ClassCastException on unmanaged instances cucumber-cdi2 ClassCastException on unmanaged instances Feb 25, 2021
@rmannibucau
Copy link
Contributor

Fix proposal looks good, do you plan to do a PR?

This was referenced Mar 9, 2021
This was referenced Mar 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants