Skip to content

integration tests and using the container's Weld context to inject recursively. #253

@tbee

Description

@tbee

I understand the need to have a standalone Weld container when doing unit tests, as weld-testing provides. But if you run integration tests for -say- a stateless EJB, you still have a junit test, but the test executes inside the JakartaEE container. Not all stateless services have a REST API. In that case you want the CDI of the container injecting things into the tests, so basically weld-junit5 without the weld-se context, but with wildfly's weld context.

I have been able to get a one level injection going, so an @Inject in a junit test is resolved. But the injected class does not have its @Inject resolved, those references are all null. I cannot seem to figure out how to make inject recursive.

This is a bit out of scope, but I dare to ask: any suggestions? 😄

BTW, this code does a one level deep injection:

        BeanManager beanManager = CDI.current().getBeanManager();
        CreationalContext<Object> ctx = beanManager.createCreationalContext(null);
        InjectionTarget<Object> injectionTarget = (InjectionTarget<Object>) beanManager
                .getInjectionTargetFactory(beanManager.createAnnotatedType(test.getClass()))
                .createInjectionTarget(null);
        injectionTarget.inject(test, ctx);

I have to admit that why this works is not obvious to me. For example InjectionTarget? It is injecting, so hardly a target of injection. Injector seems a more logical name. Alas.

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