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. >
