You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Xavier Dury <ka...@hotmail.com> on 2016/09/30 09:45:26 UTC

EJBContainerRunner and @RequestScoped

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

Re: EJBContainerRunner and @RequestScoped

Posted by Mark Struberg <st...@yahoo.de.INVALID>.
You might give DeltaSpike CdiTestRunner a try.
It cleans the contexts between test methods.

Just use cdictrl-openejb as backend.

LieGrue,
Strub

> Am 30.09.2016 um 11:45 schrieb Xavier Dury <ka...@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


Re: EJBContainerRunner and @RequestScoped

Posted by Xavier Dury <ka...@hotmail.com>.
Thanks, I did not know about ScopesRule. I don't have many tests using EJBContainer(Rule) so this is enough for me.






From: Romain Manni-Bucau <rm...@gmail.com>
Sent: Friday, September 30, 2016 11:50 AM
To: users@tomee.apache.org
Subject: Re: EJBContainerRunner and @RequestScoped
    
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



Romain Manni-Bucau (@rmannibucau) | Twitter
twitter.com
The latest Tweets from Romain Manni-Bucau (@rmannibucau). ASF / Java(EE) / Angular. LinkedIn: https://t.co/dX7XMGjbBi. JavaEE Factory: https://t.co/0VnvWwbedt . Blog ...
<https://blog-rmannibucau.rhcloud.com> | Old Wordpress Blog


RBlog: the RManniBucau v2 Blog
blog-rmannibucau.rhcloud.com
RBlog: Romain Manni-Bucau Blog about Java, JavaEE, TomEE, Angular, Open Source, Code and geeky topics...
<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 <ka...@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
    

Re: EJBContainerRunner and @RequestScoped

Posted by Romain Manni-Bucau <rm...@gmail.com>.
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 <ka...@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