1

I have an JavaEE-Application and want to use CDI in the unit-test. Currently i use org.apache.openejb.junit5.RunWithApplicationComposer and my unit tests look like this:

@RunWithApplicationComposer(mode = ExtensionMode.PER_EACH)
@Classes(cdi = true, value = {
    A.class,
    B.class,
    C.class,
    ...
    Example.class
})
public class ExampleTest {

    @Inject
    private Example testInstance;

    @Produces
    public A produceA() {
        ...
    }

    @Test
    public void test() {
        assertEquals(4, testInstance.add(2, 2));
    }

}

Here I only want to test Example.class, but for Example.class to run, A,B,C must be injectable beans.

My problem is the re-usability of the @Classes-Annotation. In my case its not only A,B,C, but about 20 classes that i need for a lot of unit tests. I don't want a @Classes annotation with 20 classes again and again at every unit-test.

I already tried a SuperClass with the @Classes-Annotation that gets extended by my unit-tests, but this doesn't work.

Do you know a more generic and reusable way to make this 20 Classes injectable beans in a unit-test?

Thanks for your help :)

Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84

1 Answers1

0

I'm not positive this will work, but can you try an abstract parent test class?

If that doesn't work, the other thing I thought of was to make a dummy bean that injects all 20 classes, then you just put that one in the Classes section:

@ApplicationScoped
public class TestClassesConfigurationBean {
 @Inject private A a;
 @Inject private B b;
.... 20x
}

@RunWithApplicationComposer(mode = ExtensionMode.PER_EACH)
@Classes(cdi = true, value = { TestClassesConfigurationBean.class }
})
public class ExampleTest {
Jonathan S. Fisher
  • 8,189
  • 6
  • 46
  • 84