Bruno,

When I run your project (assuming you meant :
https://github.com/rt15/cdi-test ) This is the exception I get, which
matches to what Gerhard is describing to you:

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:331)
at
org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.getContextualInstance(NormalScopedBeanInterceptorHandler.java:88)
at
org.apache.webbeans.intercept.RequestScopedBeanInterceptorHandler.getContextualInstance(RequestScopedBeanInterceptorHandler.java:76)
at
org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.get(NormalScopedBeanInterceptorHandler.java:70)
at
org.apache.deltaspike.testcontrol.impl.mock.SimpleMockManager$$OwbNormalScopeProxy0.getMock(org/apache/deltaspike/testcontrol/impl/mock/SimpleMockManager.java)
at
org.apache.deltaspike.testcontrol.impl.mock.MockAwareInjectionTargetWrapper.produce(MockAwareInjectionTargetWrapper.java:59)
at
org.apache.webbeans.component.AbstractOwbBean.create(AbstractOwbBean.java:122)
at org.apache.webbeans.component.ManagedBean.create(ManagedBean.java:67)
at
org.apache.webbeans.context.creational.BeanInstanceBag.create(BeanInstanceBag.java:76)
at
org.apache.webbeans.context.AbstractContext.getInstance(AbstractContext.java:159)
at org.apache.webbeans.context.AbstractContext.get(AbstractContext.java:125)
at
org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.getContextualInstance(NormalScopedBeanInterceptorHandler.java:100)
at
org.apache.webbeans.intercept.ApplicationScopedBeanInterceptorHandler.getContextualInstance(ApplicationScopedBeanInterceptorHandler.java:65)
at
org.apache.webbeans.intercept.NormalScopedBeanInterceptorHandler.get(NormalScopedBeanInterceptorHandler.java:70)
at fr.rt15.MyBean$$OwbNormalScopeProxy0.foo(fr/rt15/MyBean.java)
at fr.rt15.MyThread.run(MyThread.java:13)

On Tue, Oct 17, 2017 at 5:04 AM Bruno Boissard <[email protected]>
wrote:

> Hi Gerhard,
>
> It would make sense to start a request context in order to be able to
> access a @RequestScoped bean but my bean is @ApplicationScoped. Should it
> not be accessible without starting a request context?
>
> But while searching for my bean, in deltaspike
> MockAwareInjectionTargetWrapper, a DynamicMockManager is searched:
>
> DynamicMockManager mockManager =
> BeanProvider.getContextualReference(this.beanManager,
> DynamicMockManager.class, false);
>
> It returns a SimpleMockManager that is @RequestScoped so it cannot be used
> without a created request context.
>
> Maybe MockAwareInjectionTargetWrapper should use
> SimpleApplicationMockManager (Which is @ApplicationScoped) when we ask for
> an @ApplicationScoped bean?
>
> Regards.
>
> 2017-10-17 10:20 GMT+02:00 Gerhard Petracek <[email protected]>:
>
> > hi bruno,
> >
> > if you start your own threads, you have to control some scopes like the
> > request-scope on your own (>within< those new threads).
> > (that isn't specific to deltaspike)
> >
> > see a similar part [1] in our documentation.
> >
> > regards,
> > gerhard
> >
> > [1]
> > http://deltaspike.apache.org/documentation/container-control.html#
> > AttachaRequestContexttoaNewThreadinEE
> >
> >
> >
> > 2017-10-17 9:55 GMT+02:00 Bruno Boissard <[email protected]>:
> >
> > > Hello,
> > >
> > > I am using deltaspike 1.8.0 and owb version 1.6.3.
> > >
> > > My test which has to be executed with CdiTestRunner, is starting a new
> > > thread.
> > > This thread retrieves an instance of MyBean from CDI and try to use it.
> > > MyBean is @ApplicationScoped.
> > >
> > > It works fine unless I add in apache-deltaspike.properties:
> > > deltaspike.testcontrol.mock-support.allow_mocked_beans=true
> > > (It is necessary for some other tests. As well as stop_container=false
> is
> > > also necessary).
> > >
> > > With allow_mocked_beans=true I have following exception:
> > > javax.enterprise.context.ContextNotActiveException: WebBeans context
> > with
> > > scope type annotation @RequestScoped does not exist within current
> thread
> > >
> > > It appears that while searching for MyBean, it is searching for
> > > SimpleMockManager bean.
> > > SimpleMockManager is @RequestScoped, so deltaspike does not find it
> > > (Logical, there is no request context as we are in a new thread).
> > >
> > > A very ugly workaround is to use MyBean in MyTest: once it is created,
> it
> > > become accessible in the other thread...
> > > Is there a better solution?
> > >
> > > A maven project corresponding to this available in github, please
> search
> > > for rt15/cdi-test.
> > >
> > >
> > > Otherwise here is the source code:
> > > =======================================================
> > > @RunWith(CdiTestRunner.class)
> > > public class MyTest {
> > >
> > >     @Inject
> > >     private MyBean myBean;
> > >
> > >     @Test
> > >     public void test() throws Exception {
> > >
> > >         // Ugly workaround: uncomment next line.
> > >         // this.myBean.foo();
> > >
> > >         MyThread myThread = new MyThread();
> > >         myThread.start();
> > >         myThread.join();
> > >         if (myThread.exception != null) {
> > >             throw myThread.exception;
> > >         }
> > >     }
> > > }
> > > =======================================================
> > > public class MyThread extends Thread {
> > >
> > >     public Exception exception;
> > >
> > >     @Override
> > >     public void run() {
> > >         try {
> > >             MyBean myBean = CDI.current().select(MyBean.class).get();
> > >             myBean.foo();
> > >         } catch (Exception e) {
> > >             this.exception = e;
> > >         }
> > >     }
> > > }
> > > =======================================================
> > > @ApplicationScoped
> > > public class MyBean {
> > >
> > >     public void foo() {
> > >         System.out.println("foo");
> > >     }
> > > }
> > > =======================================================
> > > deltaspike.testcontrol.mock-support.allow_mocked_beans=true
> > > =======================================================
> > >
> > > Regards.
> > >
> >
>

Reply via email to