Maven:使用openejb运行测试时的javax.enterprise.context.ContextNotActiveException

时间:2015-07-31 12:59:33

标签: java-ee junit cdi openejb openwebbeans

我已经在OpenEjb和带有嵌入式容器的Junit的帮助下为我的Java-ee项目创建了一些单元和集成测试。测试在Eclipse中正常运行,bean(CDI / EJB)被注入并且工作正常。但是当我用maven和surefire开始测试时,我收到以下错误:

WARNING: Handling the following exception
Throwable occurred: javax.enterprise.context.ContextNotActiveException: WebBeans context with scope type annotation @RequestScoped does not exist within current thread
at org.apache.webbeans.container.BeanManagerImpl.getContext(BeanManagerImpl.java:330)
at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.getContextualInstance(NormalScopedBeanInterceptorHandler.java:88)
at org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.get(NormalScopedBeanInterceptorHandler.java:70)
at org.apache.webbeans.conversation.ConversationImpl$$OwbNormalScopeProxy0.isTransient(org/apache/webbeans/conversation/ConversationImpl.java)
at org.apache.wicket.cdi.ConversationPropagator.onUrlMapped(ConversationPropagator.java:322)
at org.apache.wicket.request.cycle.RequestCycleListenerCollection$9.notify(RequestCycleListenerCollection.java:208)

可能有什么不对?它看起来像是一个类路径问题,但显式添加openwebbeans-spi和openwebbeans-impl并不起作用。

我在测试类中使用以下行:

@ClassRule
public static final EJBContainerRule CONTAINER_RULE = new EJBContainerRule();

@Rule
public final InjectRule rule = new InjectRule(this, CONTAINER_RULE);

我还有以下的openejb-junit.properties:

openejb.validation.output.level=VERBOSE
openejb.deployments.classpath.include=.*org.mine.*
openejb.descriptors.output=true
openejb.ejbcontainer.close=single-jvm
java.naming.factory.initial=com.ibm.websphere.naming.WsnInitialContextFactory

感谢您的帮助。

问候, Huls公司。

1 个答案:

答案 0 :(得分:0)

我不确切知道你是如何创建/解析你的bean的,但这听起来像我们在测试设置中遇到的类似问题。当您使用bean管理器手动解析bean时,它与创建上下文无关,您必须手动执行此操作。

我曾经在测试代码中使用CDI编写了一个小实用程序方法来检索bean:

public <T> T getCDIBean(Class<T> type, AnnotationLiteral<?>... qualifierAnnotations) {
    Set<Bean<?>> beans = getBeanManager().getBeans(type, qualifierAnnotations);

    Bean<?> bean;
    if (beans.size() == 1) {
        bean = beans.iterator().next();
    } else {
        bean = getBeanManager().resolve(beans);
    }

    CreationalContext<?> context = getBeanManager().createCreationalContext(bean);
    return (T) getBeanManager().getReference(bean, type, context);
}