How to bootstrap weld-se in a JUnit test

不羁岁月 提交于 2019-12-05 18:13:44

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
LightGuard

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

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.

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