How to boot weld in JUnit test

I have a maven project for unit tests and would like to use CDI. I put the weld dependency in pom.xml as follows:

<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 load the 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 the unit test that this runner uses. The test introduces the scope of the bean. The problem is that the weld cannot be initialized due to the “Unsatisfied dependency” on a single injection point, as if my scope of the bean was completely unknown for welding. But this bean is in src / test / java / ... with my test (but in a different java package).

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

I noticed that at startup the welder warns, but I do not think this is 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?

+4
source share
3 answers

Check out the CDI-Unit . It provides Runner classes 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 Module User Guide .

The CDI-Unit also writes warnings below, but despite this, 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 
+6
source

A few things to look at: the Weld SE container for Arquillian or the DeltaSpike CdiCtrl Module

+4
source

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

 <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> 

Reason for warnings: The javax.ejb.PostActivate and javax.ejb.PrePassivate not found. You are lacking in addiction.

Add this dependency to your pom.xml:

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

Sincerely.

0
source

Source: https://habr.com/ru/post/1442164/


All Articles