4

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 ?

baraber
  • 3,296
  • 27
  • 46

3 Answers3

6

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
palacsint
  • 28,416
  • 10
  • 82
  • 109
4

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

Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
LightGuard
  • 5,298
  • 19
  • 19
  • I just tried with delta-spike and got the exact same error, but on the cdiContainer.boot() call. There must be something I do wrong... – baraber Oct 26 '12 at 13:41
  • Looks like how you're using it, Weld doesn't know what classes to scan. – LightGuard Oct 26 '12 at 19:14
  • indeed, but should I have to tell weld what to scan ? When the tests are lauched, all classes are under myproject/target/test-classes, along with the META-INF/beans.xml. Could it be because none of the classes are in jar files ? ( I think not but maybe I'm wrong) – baraber Oct 26 '12 at 20:09
  • You're building this up from the test classpath. You'll have to see if the beans.xml is in the correct location on the classpath. – LightGuard Oct 26 '12 at 22:44
1

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.

András
  • 26
  • 3