为了能够在我的http://jglue.org/cdi-unit/ 4测试中使用CDI,我使用了CDI,我注入了JUnit并调用了一个方法来持久化一个Client对象,但是我得到了以下错误:
java.lang.NoClassDefFoundError org/jboss/weld/environment/se/Weld我的无状态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) {
}
}
}我的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);
}
}我没有使用Maven,我已经下载了weld-se-core-2.3.0.Final.jar,并发现了另一个错误:
java.lang.NoClassDefFoundError: org/jboss/weld/environment/ContainerInstanceFactory编辑:
现在我正在使用Maven,这是我的JUnit测试代码:
OpTest.java:
@RunWith(CdiRunner.class)
@AdditionalClasses({OperationsEJB.class})
@SupportEjb
public class OpTest {
@Inject
OperationsEJB ejb;
@Test
public void test() {
assertNotNull(ejb);
}
}以下是我的EJB:
@Stateless
public class OperationsEJB {
@PersistenceContext(unitName = "db_PU")
EntityManager em;
public void addClient(Client client) {
em.persist(client);
}
}当我运行测试时,我会得到以下错误:
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.JUnit日志中显示的错误是:
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]提前谢谢你。
发布于 2017-07-13 15:03:14
@Ejb的注释替换使得应该注入的bean具有注释@Default是必要的。
@Stateless
@Default
public class OperationsEJB {应该能帮上忙。
也许:ejb单元可以帮上忙。我们也有类似的问题,并为此在cdi单元之上创建了一个额外的模块。
https://stackoverflow.com/questions/33188861
复制相似问题