1

I have a JEE project that is deployed on wildfly 10 smoothly.

I'm trying to write junit tests for my CDI beans with cdi-unit library (http://bryncooke.github.io/cdi-unit/)

My pom.xml looks like the following:

<dependency>
    <groupId>javax</groupId>
    <artifactId>javaee-api</artifactId>
    <version>7.0</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>

<dependency>
     <groupId>org.jglue.cdi-unit</groupId>
     <artifactId>cdi-unit</artifactId>
     <version>4.0.1</version>
     <scope>test</scope>
</dependency>

However my sample test:

@RunWith(CdiRunner.class)
public class SampleTest {

   @Inject
   private Foo foo;

   @Test
   public void testFoo(){
      foo.bar();
   }
}

fails to initialize due to missing weld class:

java.lang.NoClassDefFoundError: org/jboss/weld/environment/se/Weld
    at java.lang.Class.getDeclaredConstructors0(Native Method)
    at java.lang.Class.privateGetDeclaredConstructors(Class.java:2671)
    at java.lang.Class.getConstructor0(Class.java:3075)
    at java.lang.Class.getConstructor(Class.java:1825)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:84)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:70)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:43)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:678)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.ClassNotFoundException: org.jboss.weld.environment.se.Weld
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 17 more

CDI-UNIT manual says here that it automatically uses it's own version of Weld, hence there is no need to explicitly add any weld libraries in pom.xml.

Any help please?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
user711189
  • 4,383
  • 4
  • 30
  • 48
  • Check the 'dependency tree' with maven to see it actually IS added – Kukeltje Feb 15 '18 at 12:19
  • already checked. cdi-unit does not fetch any weld libraries – user711189 Feb 15 '18 at 12:20
  • If I may suggest an alternative approach, **try [weld-junit](https://github.com/weld/weld-junit) instead**. It supports both, JUnit4 and JUnit5 and it designed for unit-testing your CDI beans. It basically bootstraps Weld SE container in customizable way in order to let you use real beans in those tests, but it allows for simple mocking too if that's what you need. – Siliarus Feb 15 '18 at 14:12
  • @geo Sorry, that bit of the manual is no longer true as of CDI-Unit 4. That section has now been removed, and the quickstart updated, because you now have to specify your own version of Weld SE. (The reason was to do with supporting Weld 1, 2 or 3 in the same code base.) – seanf Aug 29 '18 at 02:44

1 Answers1

2

You should include weld-se into your maven dependencies.

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

add as is fitting the version your wildfly-weld uses.

If you want more than the Simulation of the CDI-Container but also some have a realistic simulation of EJB-Functionality like persistence, transactionality, message driven beans, try ejb-cdi-unit It is derived from cdi-unit. There you can also find some example-projects which might help with your cdi-unit-issues.

aschoerk
  • 3,333
  • 2
  • 15
  • 29