weld / weld-testing

Set of test framework extensions (JUnit 4, JUnit 5, Spock) to enhance the testing of CDI components via Weld. Supports Weld 5.
http://weld.cdi-spec.org/
Apache License 2.0
102 stars 30 forks source link

Method parameter injection fails with parametrized argument types #149

Closed JHahnHRO closed 1 year ago

JHahnHRO commented 1 year ago

The following three test fail:

@ApplicationScoped
public class MapProducer {

    @Produces
    @Dependent
    public <T> Map<String,T> produceMap(InjectionPoint ip) {
        Map<String, T> map = new HashMap<>();
        map.put(ip.getType().getTypeName(), null);
        return map;
    }
}

@EnableWeld
class TestParameterInjectionWithParametrizedTypes {
   @Test
   public void testparamInjectionWithEvent(Event<String> stringEvent) {
       Assertions.assertNotNull(stringEvent);
   }
   @Test
   public void testparamInjectionWithInstance(Instance<String> stringInstance) {
       Assertions.assertNotNull(stringInstance);
   }
   @Test
   public void testparamInjectionWithOtherParametrizedType(Map<String, Integer> map) {
       Assertions.assertNotNull(map);
       Assertions.assertTrue(map.containsKey("java.util.Map<java.lang.String, java.lang.Integer>"));
   }
}

The reason is that WeldJunit5Extension uses getContainerFromStore(extensionContext).select(parameterContext.getParameter().getType(), ... instead of getContainerFromStore(extensionContext).select(parameterContext.getParameter().getParametrizedType(), ...

manovotn commented 1 year ago

Thanks for issue and PR @JHahnHRO! This is a good find that slipped us, I am surprised it hasn't come up sooner.

We can definitely have this fixed on current main branch but older versions are no longer maintained.

manovotn commented 1 year ago

Fixed via https://github.com/weld/weld-testing/pull/152

@JHahnHRO what version are you using? Is this blocking you on 4.0.0.Final?

JHahnHRO commented 1 year ago

I'm using 2.0.2 regularly, because we're still using javax. namespaces at work. We're will be migrating to jakarta., Wildfly 27+, Quarkus 3, ... next year I hope and I will push for it. However, it's not guaranteed. For the next few months at least, we're stuck with EE8. And no, it's not blocking per se. I can work around it by using programmatic injection.

manovotn commented 1 year ago

Ok, if it were blocking you in some way, do come back and we can look for a solution. Otherwise, I'd rather keep those old versions dormant :)