Weld-Junit5: addPackages doesn't add all contained beans to the test container

282 Views Asked by At

I'm trying to migrate from Java EE 8 to Jakarta EE 10 and was running a JUnit 5 test with Weld. I created a simple example with JUnit5 and Weld (cdi) could not resolve the (inner) injected classes.

The test failed with: WELD-001408: Unsatisfied dependencies ...

Example class (which injects the ExampleHelper class):

public class Example {
    @Inject
    ExampleHelper exampleHelper;

    public void getHelloText() {
        exampleHelper.greet();
    }
}

ExampleHelper class:

public class ExampleHelper {
    public void greet() {
        System.out.println("Hello");
    }
}

JUnit5 Test:

@ExtendWith(WeldJunit5Extension.class)
class ExampleTest {

    @WeldSetup
    public WeldInitiator weld = WeldInitiator.of(WeldInitiator.createWeld()
            .addBeanClass(Example.class)
            .addPackages(false, Example.class.getPackage())
    );

    @Inject
    Example example;

    @Test
    public void test() {
        example.getHelloText();
    }
}

I would have expected that by adding the package, all classes within that package could be found and injected. (This is how I did it before with JEE8).

I get the error:

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type ExampleHelper with qualifiers @Default
  at injection point [BackedAnnotatedField] @Inject com.company.wf28test.Example.exampleHelper
  at com.company.wf28test.Example.exampleHelper(Example.java:0)

Only if I explicitly add all injected classes individually Weld can find them.

.addBeanClass(Example.class)
.addBeanClass(ExampleHelper.class)

then the test runs through

Dependencies:

jakarta.jakartaee-api 10.0.0
junit-jupiter-api 5.9.3
junit-jupiter-engine 5.9.3
weld-junit5 4.0.0.Final

What is my mistake or misunderstanding?

1

There are 1 best solutions below

0
On

Due to technical and time constraints I'm unable to test this with the versions of the libraries you posted, but I ran into the same problem using weld-junit5 2.0.0.Final and could solve it by ommiting the getPackage() call.

I.e. you could try

        .addPackages(false, Example.class)

instead of

        .addPackages(false, Example.class.getPackage())