Huls
Huls

Reputation: 11

Maven: javax.enterprise.context.ContextNotActiveException while running tests with openejb

I've created some unit- and integration tests for mine Java-ee project with the help of OpenEjb and Junit with an embedded container. The tests are running fine in Eclipse, beans(CDI/EJB) are injected and working fine. But when I start the tests with maven and surefire, I get the following error:

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)

What could be wrong? It looks like a classpath issue or something, but explicit adding openwebbeans-spi and openwebbeans-impl didn't work.

I use the following lines in the testclass:

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

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

And I've the following 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

Thanks for any help.

Greetings, Huls.

Upvotes: 1

Views: 575

Answers (1)

shillner
shillner

Reputation: 1846

I don't know exactly how you are creating/resolving your beans but it sounds like a similar problem we had in our test setup. When you resolve your bean manually using the bean manager it is not associated with a creational context and you will have to do this manually.

I once wrote a small utility method for retrieving beans using CDI in our test code:

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);
}

Upvotes: 0

Related Questions