Java EE JUnit ne fonctionne pas avec CDI-Unit lors de l'utilisation d'EJBs
J'utilise le JAR CDI-Unit http://jglue.org/cdi-unit/
pour pouvoir utiliser CDI dans mes tests JUnit 4, j'ai injecté mon EJB et appelé une méthode pour conserver un objet Client
, mais j'obtiens l'erreur suivante :
java.lang.NoClassDefFoundError org/jboss/weld/environment/se/Weld
Mon EJB apatride (OperationsEJB.java):
@Stateless
public class OperationsEJB {
@PersistenceContext(unitName="db_PU")
EntityManager em;
public void addClient(Client client) {
try {
em.persist(client);
} catch (Exception e) {
}
}
}
Mon test JUnit:
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);
}
}
Je n'utilise pas Maven, j'ai téléchargé le weld-se-core-2.3.0.Final.jar
et j'ai une autre erreur:
java.lang.NoClassDefFoundError: org/jboss/weld/environment/ContainerInstanceFactory
MODIFIER:
Maintenant j'utilise Maven, c'est mon code de test JUnit :
OpTest.java:
@RunWith(CdiRunner.class)
@AdditionalClasses({OperationsEJB.class})
@SupportEjb
public class OpTest {
@Inject
OperationsEJB ejb;
@Test
public void test() {
assertNotNull(ejb);
}
}
Voici mon EJB:
@Stateless
public class OperationsEJB {
@PersistenceContext(unitName = "db_PU")
EntityManager em;
public void addClient(Client client) {
em.persist(client);
}
}
Lorsque j'exécute le test, j'obtiens cette erreur:
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.
Et l'erreur affichée dans le journal JUnit est:
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]
Merci d'avance.
1 answers
Le type de remplacement d'annotation de @Ejb rend nécessaire que le bean qui doit injecter ait l'annotation @Default.
@Stateless
@Default
public class OperationsEJB {
Devrait aider.
Peut-être: ejb-cdi-unité pourrait aider. nous avons eu des problèmes similaires et avons créé un module supplémentaire au-dessus de cdi-unit pour cela.