oghenez / mycila

Automatically exported from code.google.com/p/mycila
0 stars 0 forks source link

Spring dependency reinjection #37

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago

I use Mycila testing with Spring and JUnit, and I noticed, that if i
have more than one @Test method in a class, then the dependencies are 
reinjected when executing  each test method. I can see this from a few
facts:

- I have Quartz in the context, and it starts 3 threads for one test
case, and after executing 5 test methods, I will have 5 * 3 = 15
Quartz threads
- I can see closing the persistence contexts only after executing the
last test method

The difference between executing the same tests
with SpringJUnitRunner and MycilaJunitRunner is that the
DependencyInjectionTestExecutionListener's prepareTestInstance()
method is executed before each test method thus dependencies are
reinjected for each execution by Mycila, while Spring executes
prepareTestInstance() only once before executing any test methods, and
then beforeTestMethod() for each tests, which will not reinject
dependecies. 

I use Spring 3.0.2, Mycila Testing Spring 2.4 

Original issue reported on code.google.com by band...@inf.elte.hu on 8 Nov 2011 at 12:17

GoogleCodeExporter commented 8 years ago
I've been looking a little into this.
When using just the spring testing framework you are able to use the 
DirtiesContextTestExecutionListener to signal when to refresh the context.

When using mycila you very often specify behavior for each test, and this a 
fresh mock should be injected for each testexecution (as well as closing the 
context properly after each testexecution).

I therefore propose to close the context after each testexecution, and refresh 
it before the next testexecution.

This should fix the issue, but might be slower due to more initialization.

Does this sound OK?

Note:
In the future we might support the same DirtiesContextTestExecutionListener as 
in the spring testframework for the enduser to control this better - but not at 
this stage.

Original comment by davidkar...@gmail.com on 11 Jan 2012 at 4:21

GoogleCodeExporter commented 8 years ago

Original comment by davidkar...@gmail.com on 11 Jan 2012 at 4:23

GoogleCodeExporter commented 8 years ago

Original comment by davidkar...@gmail.com on 11 Jan 2012 at 4:24

GoogleCodeExporter commented 8 years ago
Sorry to get back after quite some time. This solution seems okay. How can I 
close and refresh the context?

Original comment by band...@inf.elte.hu on 4 Apr 2012 at 4:39