tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Romain Manni-Bucau <rmannibu...@gmail.com>
Subject Re: EJBContainerRunner and @RequestScoped
Date Fri, 30 Sep 2016 09:50:56 GMT
Hi

by default it starts with the container for current thread and stops
symmetrically yes. Goal was to provide this scope by default to avoid a lot
of headache.

For application composer we have the flag
openejb.testing.start-cdi-contexts you
can set to false to prevent it. We can introduce it for EJBContainer too.
Was not done cause in practise you almost never use the starting thread
excepted in tests were EJBContainer is more or less abandonned today (there
are good alternative with arquillian, tomee embedded runner, application
composer...).

Note also that org.apache.openejb.junit.ScopesRule can help you to control
active scopes and potentially restart this request scope.



Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Wordpress Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | Tomitriber
<http://www.tomitribe.com> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2016-09-30 11:45 GMT+02:00 Xavier Dury <kalgon@hotmail.com>:

> Hi,
>
> Is it normal that all test methods from a test class running with
> EJBContainerRunner share one single request context?
> If the request context is shared between methods, this can cause some
> unwanted dependency between them.
>
> Example:
>
> public @ApplicationScoped class ScopeService {
>     public void print(String message) { System.out.println(message); }
>     public void onInitialized(@Observes @Initialized(RequestScoped.class)
> Object event) { System.out.println("initialized request context"); }
>     public void onDestroyed(@Observes @Destroyed(RequestScoped.class)
> Object event) { System.out.println("destroyed request context"); }
> }
>
> public @RunWith(EJBContainerRunner.class) class ContainerScopeTest {
>     public @Inject ScopeService service;
>     public @Test void test1() { this.service.print("test1"); }
>     public @Test void test2() { this.service.print("test2"); }
> }
>
> gives the following logs:
>
> ...
> INFO - Deployed Application(path=D:\development\workspaces\sdpsp\
> arquillian)
> initialized request context
> test2
> test1
> INFO - Undeploying app: D:\development\workspaces\sdpsp\arquillian
> destroyed request context
> INFO - Destroying OpenEJB container
>
> A test with arquillian like this one:
>
> public @RunWith(Arquillian.class) class ArquillianScopeTest {
>         public @Deployment static JavaArchive deploy() { return
> ShrinkWrap.create(JavaArchive.class).addClasses(ScopeService.class).
> addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"); }
>         public @Inject ScopeService service;
>         public @Test void test1() { this.service.print("test1"); }
>         public @Test void test2() { this.service.print("test2");        }
> }
>
> gives the following logs:
>
> ...
> INFO - Deployed Application(path=D:\development\workspaces\sdpsp\
> arquillian\9ee795c0-40e2-47b9-b384-c923f525aefd.jar)
> initialized request context
> test1
> destroyed request context
> initialized request context
> destroyed request context
> initialized request context
> test2
> destroyed request context
> initialized request context
> destroyed request context
> INFO - Undeploying app: D:\development\workspaces\
> sdpsp\arquillian\9ee795c0-40e2-47b9-b384-c923f525aefd.jar
> INFO - Destroying container system
>
> There are too many request context initialized/destroyed with arquillian
> but at least each test gets its own context.
>
> With ApplicationComposer, a new container is started/stopped for each each
> test so they each run with their own request context.
>
> Xavier

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message