You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Transaction is not started when two spring contexts are created during tests using AspectJ load time weaving [SPR-7238] #11897
I run unit tests using Spring integration testing tools, that is, marking test classes with @ContextConfiguration(locations = {...}) , which implies that Spring uses the same context for all test having the same context location configuration. In the tests, I call some transactional service. Transactions are weaved during compile time with AspectJ.
The problem is following: as long as there is only one Spring context created during tests (all test classes using the some context location configuration, so there is only one context created), everything runs smoothly. But when one test uses different context configuration, and there are two contexts created (which should be independent from each other), transactions don't get started, and I got exception "no transaction in progress...".
It looks like two contexts that should be independent, actually have some impact on each other. More precisely, it works this way: tests methods based on first spring contexts pass, then test methods using second context are run, and they pass, but then another test methods using first spring context are run and they fail with "No transaction" exception. So it look like running second context spoils the first one.
This is serious problem for us, which makes running tests very difficult. JUnit doesn't let us decide on the order in which test methods are run, which makes this really problematic.
I attach simple test case as maven project, run the test and see that they fail (if they are run in order: test 1, 2, 3). If you change the second test so that is uses the same context configuration like tests 1 and 3, everything works fine. But it test 2 uses different configuration, test 3 fails.
I'm not quite sure that statement "should be independent from each other" is correct. One test can modify context and this modification can affect other tests. And that why @DirtiesContext annotation exists. Have you tried applying this annotation on your tests classes?
From what I remember, the case was that the tests didn't modify the context, so @DirtiesContext is invalid here. Simply when the second context got started, the first one ceased to work properly. We found out that it is because when you use AspectJ based waving, the aspects use some global, static code to get the reference to the context. So each newly instantiated context makes other contexts broken.
The workaround for this was to call some Spring-aspectJ specific code at the beginning of each test, that resets the global pointer so that it points at the context that is going to be used in that test.
Finally we switched back to proxy-based AOP (because AspectJ-waved domain objects had terrible performance), and the problem doesn't occur anymore, because no static pointing to context is involved.
Activity
spring-projects-issues commentedon May 26, 2010
Grzegorz Borkowski commented
test case
spring-projects-issues commentedon Mar 3, 2011
Igor Mihalik commented
I'm not quite sure that statement "should be independent from each other" is correct. One test can modify context and this modification can affect other tests. And that why
@DirtiesContextannotation exists. Have you tried applying this annotation on your tests classes?spring-projects-issues commentedon Mar 3, 2011
Grzegorz Borkowski commented
From what I remember, the case was that the tests didn't modify the context, so
@DirtiesContextis invalid here. Simply when the second context got started, the first one ceased to work properly. We found out that it is because when you use AspectJ based waving, the aspects use some global, static code to get the reference to the context. So each newly instantiated context makes other contexts broken.The workaround for this was to call some Spring-aspectJ specific code at the beginning of each test, that resets the global pointer so that it points at the context that is going to be used in that test.
Finally we switched back to proxy-based AOP (because AspectJ-waved domain objects had terrible performance), and the problem doesn't occur anymore, because no static pointing to context is involved.
spring-projects-issues commentedon Jun 18, 2012
Sam Brannen commented
This is a duplicate of #11019.