1 Reply Latest reply on May 27, 2016 7:22 AM by Tomas Remes

    Recommended ways to use WELD in JUnit (SE), Tomcat (Servlet) and JBoss

    Alain Pannetier Newbie

      I have project relying on CDI in two places.

      The target environment of the project is EAP 6.2 but we're also targeting a "lighter" tomcat 7 environment.

      In addition each maven module has its bunch of associated JUnit tests.

      All these executions will leverage WELD at some point.

       

      1/ back end adapters are discovered at run time by their adapter manager (named core)

      2/ JSF based (stored in jars) GUI contributions are also discovered at run time by their shell GUI (the war end product).

       

      With a bottom up approach we tested and used CDI 1.1 based on WELD 2.3.4 for adapter discovery.

      The dependency was on the SE flavour:

       

          <dependency>
            <groupId>org.jboss.weld.se</groupId>
            <artifactId>weld-se</artifactId>
            <version>2.3.4</version>
          </dependency>
      

       

      So far so good, till the day when silly me deployed in tomcat and the weld-se jar triggered a clash with tomcat's own javax.el classes.

      I got away with it by playing on the maven dependency exclusions and adding the weld-servlet dependency on the war module

       

       

          <dependency>
            <groupId>com.acme.proj</groupId>
            <artifactId>core</artifactId>
            <version>1.0</version>
            <scope>runtime</scope>
            <exclusions>
              <exclusion>
                <groupId>org.jboss.weld.se</groupId>
                <artifactId>weld-se</artifactId>
              </exclusion>
            </exclusions>
          </dependency>
      
          <dependency>
            <groupId>org.jboss.weld.servlet</groupId>
            <artifactId>weld-servlet</artifactId>
            <version>2.3.4</version>
          </dependency>
      

       

      But I forgot that in the code we're explicitly doing things like

       

      import org.jboss.weld.environment.se.Weld;
      import org.jboss.weld.environment.se.WeldContainer;
      
      [...]
      
          private AdapterRegister   adapterRegister;
          private Weld              weld;
          private WeldContainer     container;
      
      [...]
              this.weld = new Weld();
              this.container = weld.initialize();
              adapterRegister = this.getBean(AdapterRegister.class);
      

       

      So I'm looking for suggestions:

       

      Ideally I would need to still be able to run my unit tests, many of them implicitly rely on CDI.

      Yet also be able to write portable code (previous code was obviously not portable) that can be included in a war...

      that can run on both JBoss and tomcat...