Issue 1:
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:

springtestcase.zip (17.86 kB)

Issue Links:

#11019 TestContext framework should support one AspectJ instance per ApplicationContext ("duplicates")

1 votes, 3 watchers

Issue 2:
John Wu opened SPR-6353 and commented

The Symptom

Each test passes when running individually;

A few @Transactional tests may fail when running in a test suite. The failure may occur on any @Transactional test class, depending on the nondeterministic runtime tests sequence. In fact, it's quite difficult to reproduce before knowing the root cause.

Steps to Reproduce

Using Spring 2.5.6 or 3.0.0.RC1, JUnit 4;

Create a SimpleDbOpService (may or may not annotated with @Transactional), add a method to insert a record into DB based on the input parameters;

Create a SimpleDbOpATest:

@ContextConfiguration({"base.xml", "A.xml"})

Create a SimpleDbOpBTest:

@ContextConfiguration({"base.xml", "B.xml"})

Create a SimpleDbOpCTest:

@ContextConfiguration({"base.xml", "A.xml"})

Each test is annotated with @Transactional, calling the SimpleDbOpService and verifying the DB operation results at the end of test method;

<tx:annotation-driven mode="aspectj"/> in base.xml, also add "transactionManager" definition in

Create a test suite class as the following:

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
SimpleDbOpATest.class,
SimpleDbOpBTest.class,
SimpleDbOpCTest.class
})
public class Debug {

}

The tests in SimpleDbOpCTest will fail.

Root Cause

In the Test Framework, it creates one instance of ApplicationContext for each "Set of context locations" and caches (and switches) the ApplicationContext instances by "context locations". Among all those ApplicationContext instances, the instance of AnnotationTransactionAspect is shared. That is, during the test suite running, there will be only one instance of AnnotationTransactionAspect, no matter how many ApplicationContext instances created.

A reference to BeanFactory (in spring 3.0.0.RC) or transactionManager (in spring 2.5.6) will be injected into AnnotationTransactionAspect (derived from TransactionAspectSupport) while the ApplicationContext being loaded.

In the example above, test A and C have the exactly same context locations, and they will share the same application context instance when running in a test suite. So, when running tests A, B, and C in a suite and in that order, the application context instance switches from A to B to A. However, the transactionManager instance (retrieved from an instance of AnnotationTransactionAspect) will be A, B, and B (should be A though), because there is only one instance of AnnotationTransactionAspect per class loader. In turn, that causes the operations in the C.testXxx() be split in to two transactionManager instances, one from AnnotationTransactionAspect and the other from ApplicationContext. Therefore, the DB result verification fails.

Proposed Solution

To create one Aspect instance per ApplicationContext, not per class loader. Not sure if that's achievable though.

Further Resources

Spring Test Context Caching + AspectJ @Transactional + Ehcache pain blog by Java Code Geeks

Related Issues

#11897

#17123

#10789

#12619

Affects: 2.5.6, 3.0.5

Reference URL: http://forum.springsource.org/showthread.php?t=79949

5 votes, 7 watchers