Skip to content

Transaction is not started when two spring contexts are created during tests using AspectJ load time weaving [SPR-7238] #11897

Closed

Description

@spring-projects-issues

Grzegorz Borkowski opened SPR-7238 and commented

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.


Affects: 3.0.2

Reference URL: http://forum.springsource.org/showthread.php?p=301948#post301948

Attachments:

Issue Links:

1 votes, 3 watchers

Activity

  1. spring-projects-issues commented on May 26, 2010

    @spring-projects-issues
    CollaboratorAuthor

    Grzegorz Borkowski commented

    test case

  2. spring-projects-issues commented on Mar 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    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 @DirtiesContext annotation exists. Have you tried applying this annotation on your tests classes?

  3. spring-projects-issues commented on Mar 3, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Grzegorz Borkowski commented

    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.

  4. spring-projects-issues commented on Jun 18, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    This is a duplicate of #11019.

  5. added
    type: bug
    in: test
    in: core
    status: duplicate
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corein: teststatus: duplicate

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions