3

I'm using the CDI-Unit JAR http://jglue.org/cdi-unit/ to be able to use CDI in my JUnit 4 tests, I've injected my EJB and called a method to persist a Client object, but I get the following error :

java.lang.NoClassDefFoundError org/jboss/weld/environment/se/Weld

My Stateless EJB (OperationsEJB.java) :

@Stateless
public class OperationsEJB {

    @PersistenceContext(unitName="db_PU")
    EntityManager em;

    public void addClient(Client client) {

        try {
            em.persist(client);
        } catch (Exception e) {

        }
    }
}

My JUnit test :

import static org.junit.Assert.*;
import javax.inject.Inject;
import org.jglue.cdiunit.AdditionalClasses;
import org.jglue.cdiunit.CdiRunner;
import org.jglue.cdiunit.ejb.SupportEjb;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(CdiRunner.class)
@AdditionalClasses(OperationsEJB.class)
@SupportEjb
public class TestApp {

    @Inject
    OperationsEJB ejb;

    @Test
    public void test() {

        Client c1 = new Client();
        c1.setNomClient("client1");

        ejb.addClient(c1);

        assertNotNull(c1);
    }

}

I'm not using Maven, I've downloaded the weld-se-core-2.3.0.Final.jar and I got another error :

java.lang.NoClassDefFoundError: org/jboss/weld/environment/ContainerInstanceFactory

EDIT :

Now I'm using Maven, this is my JUnit test code :

OpTest.java :

@RunWith(CdiRunner.class)
@AdditionalClasses({OperationsEJB.class})
@SupportEjb
public class OpTest {

    @Inject
    OperationsEJB ejb;

    @Test
    public void test() {


        assertNotNull(ejb);
    }

}

Here is my EJB :

@Stateless
public class OperationsEJB {

    @PersistenceContext(unitName = "db_PU")
    EntityManager em;

    public void addClient(Client client) {

        em.persist(client);
    }
}

When I run the test I get this error :

Oct 18, 2015 3:59:04 PM org.jboss.weld.bootstrap.WeldStartup <clinit>
INFO: WELD-000900: 2.2.9 (Final)
Oct 18, 2015 3:59:05 PM org.jboss.weld.bootstrap.WeldStartup startContainer
INFO: WELD-000101: Transactional services not available. Injection of @Inject UserTransaction not available. Transactional observers will be invoked synchronously.
Oct 18, 2015 3:59:06 PM org.jboss.weld.event.ExtensionObserverMethodImpl checkRequiredTypeAnnotations
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] org.jglue.cdiunit.internal.TestScopeExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.
Oct 18, 2015 3:59:06 PM org.jboss.weld.event.ExtensionObserverMethodImpl checkRequiredTypeAnnotations
WARN: WELD-000411: Observer method [BackedAnnotatedMethod] public org.jglue.cdiunit.internal.ejb.EjbExtension.processAnnotatedType(@Observes ProcessAnnotatedType<Object>) receives events for all annotated types. Consider restricting events using @WithAnnotations or a generic type with bounds.

and the error shown in JUnit log is :

org.jboss.weld.exceptions.DeploymentException: WELD-001408: Unsatisfied dependencies for type OperationsEJB with qualifiers @Default
  at injection point [UnbackedAnnotatedField] @Inject @EJB proj.OpTest.ejb
  at proj.OpTest.ejb(OpTest.java:0)
WELD-001475: The following beans match by type, but none have matching qualifiers:
  - Managed Bean [class com.proj.EJB.OperationsEJB] with qualifiers [@EJbQualifier @Any]

Thank you in advance.

Jens Piegsa
  • 7,399
  • 5
  • 58
  • 106
Dwix
  • 1,139
  • 3
  • 20
  • 45
  • Any particular reason why you do not use maven (or any other dependency management tool)? – Jan Galinski Oct 18 '15 at 10:43
  • @JanGalinski I'm still a beginner and I find it a little complicated to get used to. Should I really need to learn how to use it? Should I use it in all my projects? – Dwix Oct 18 '15 at 14:08
  • 1
    As you already noticed, setting up all dependencies of non-trivial java systems (like ejb/cdi) manually is difficult. You should not have to fight this, since someone else already provides a maven pom containing the correct setp that you just can use. So yes: learn maven and yes, use it! – Jan Galinski Oct 18 '15 at 14:23
  • @JanGalinski Thank you, now I'm using maven, I've added all the dependencies needed, but I get the some errors while trying the test if my injected ejb is null. I've edit my post and added the code & errors. Thanks in advance. – Dwix Oct 18 '15 at 15:06

1 Answers1

0

the kind of annotation replacement of @Ejb makes it necessary, that the bean which should inject has the annotation @Default.

@Stateless
@Default 
public class OperationsEJB {

should help.

Perhaps: ejb-cdi-unit could help. we had similar problems and created an extra module on top of cdi-unit for this.

aschoerk
  • 3,333
  • 2
  • 15
  • 29