Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to bootstrap weld-se in a JUnit test

Tags:

java

junit

cdi

weld

I have a maven project for unit tests and would like to use CDI. I've put the weld-se dependency in pom.xml like this :

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.10</version>
</dependency>
<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se</artifactId>
    <version>1.1.8.Final</version>
</dependency>
<dependency>
    <groupId>javax.enterprise</groupId>
    <artifactId>cdi-api</artifactId>
    <version>1.0-SP3</version>
</dependency>

I'm bootstrapping weld in a JUnit test runner :

public class WeldJUnit4Runner extends BlockJUnit4ClassRunner {
   private final Class klass;
   private final Weld weld;
   private final WeldContainer container;

   public WeldJUnit4Runner(final Class klass) throws InitializationError {
       super(klass);
       this.klass = klass;
       this.weld = new Weld();
       this.container = weld.initialize();
   }

   @Override
   protected Object createTest() throws Exception {
       final Object test = container.instance().select(klass).get();

       return test;
   }
}

And a unit test that uses this runner. The test is injecting an application scoped bean. The problem is that weld cannot initialize because of an "Unsatisfied dependency" on the only injection point, as if my application scoped bean was totally unknown to weld. But that bean is in src/test/java/... with my test (but in another java package).

I have an empty beans.xml in src/test/resources.

I noticed weld gives warnings when starting but I don't think these are the cause of my problem :

604 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PostActivate' not found, interception based on it is not enabled
605 [main] WARN org.jboss.weld.interceptor.util.InterceptionTypeRegistry - Class 'javax.ejb.PrePassivate' not found, interception based on it is not enabled

Can someone help me with that ?

like image 363
baraber Avatar asked Dec 20 '22 14:12

baraber


2 Answers

Have a look at CDI-Unit. It prodives a Runner for JUnit Test classes:

@RunWith(CdiRunner.class) // Runs the test with CDI-Unit
class MyTest {
    @Inject
    Something something; // This will be injected before the tests are run!

    ...
}

Source: CDI-Unit user guide.

CDI-Unit also logs the warnings below but despite that it works well:

WARN (InterceptionTypeRegistry.java) - WELD-001700: Interceptor annotation class javax.ejb.PostActivate not found, interception based on it is not enabled
WARN (InterceptionTypeRegistry.java) - WELD-001700: Interceptor annotation class javax.ejb.PrePassivate not found, interception based on it is not enabled
like image 104
palacsint Avatar answered Jan 03 '23 02:01

palacsint


Couple of things to look at: Weld SE container for Arquillian or the DeltaSpike CdiCtrl module

like image 43
LightGuard Avatar answered Jan 03 '23 03:01

LightGuard