How to bootstrap weld-se in a JUnit test

女生的网名这么多〃 提交于 2019-12-07 08:30:52

问题


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 ?


回答1:


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



回答2:


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




回答3:


Add the following beans.xml to the src/test/resources/META-INF directory:

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
    version="1.1" bean-discovery-mode="all">
</beans>

The reason for the warnings: Classes javax.ejb.PostActivate and javax.ejb.PrePassivate were not found. You are missing a dependency.

Add this dependency to your pom.xml:

<dependency>
    <groupId>javax.ejb</groupId>
    <artifactId>javax.ejb-api</artifactId>
    <version>3.2</version>
</dependency>

Regards.



来源:https://stackoverflow.com/questions/13076189/how-to-bootstrap-weld-se-in-a-junit-test

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!