Java EE JUnit在使用EJB时​​不使用CDI-Unit

时间:2015-10-17 16:23:56

标签: maven junit ejb cdi cdi-unit

我使用CDI-Unit JAR http://jglue.org/cdi-unit/能够在我的JUnit 4测试中使用CDI,我已经注入了我的EJB并调用了一个持久化{{1}的方法} object,但是我收到以下错误:

Client

My Stateless EJB(OperationsEJB.java):

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

我的JUnit测试:

@Stateless
public class OperationsEJB {

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

    public void addClient(Client client) {

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

        }
    }
}

我没有使用Maven,我已经下载了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); } } ,我又收到了另一个错误:

weld-se-core-2.3.0.Final.jar

编辑:

现在我使用Maven,这是我的JUnit测试代码:

OpTest.java:

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

这是我的EJB:

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

    @Inject
    OperationsEJB ejb;

    @Test
    public void test() {


        assertNotNull(ejb);
    }

}

当我运行测试时,我收到此错误:

@Stateless
public class OperationsEJB {

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

    public void addClient(Client client) {

        em.persist(client);
    }
}

并且JUnit日志中显示的错误是:

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.

提前谢谢。

1 个答案:

答案 0 :(得分:0)

@Ejb的注释替换类型使得必须注入的bean具有注释@Default。

@Stateless
@Default 
public class OperationsEJB {

应该有帮助。

也许:ejb-cdi-unit可以提供帮助。我们遇到了类似的问题,并在cdi-unit上创建了一个额外的模块。