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
Transactional JUnit test with timeout does not create a transaction [SPR-4541] #9218
For test methods with timeout specified, JUnit 4 unfortunately has the default behavior of executing befores/afters in the main thread while executing the actual test method in a separate thread... Within Spring, that behavior was retained for JUnit-specified timeouts, while things are all executing in the main thread in case of a Spring-specified timeout (@Timed, which you could use as a workaround for the time being).
As of Spring 2.5.3, SpringJUnit4ClassRunner always executes befores/afters in the same thread as the test method. As a consequence, JUnit-specified timeouts will work fine in combination with Spring transactions now. Note that JUnit-specified timeouts still differ from Spring-specific timeouts in that the former execute in a separate thread while the latter simply execute in the main thread (like regular tests).
This limitation is actually documented in the reference documentation, hence this is rather an improvement than a bug. The documentation suggests to use Spring's @Timed instead.
There's also one important change in semantics here: Befores/afters are now part of the JUnit-specified timeout whereas previously only the actual test method execution counted for the timeout. This should be ok for all typical use cases but it's worth noting...
There's unfortunately a concurrency issue hiding in TransactionalTestExecutionListener which previously didn't expect to be called for interleaved transactions from different threads (which may happen now that JUnit-style timeouts are supported for transactions as well). This will impact tests that fail because of a timeout. I'm working on revising TransactionalTestExecutionListener accordingly.
Activity
spring-projects-issues commentedon Mar 10, 2008
Juergen Hoeller commented
That's probably due to timeout-affected test methods being executed in a separare thread. I'll research this for 2.5.3.
Juergen
spring-projects-issues commentedon Mar 26, 2008
Juergen Hoeller commented
For test methods with timeout specified, JUnit 4 unfortunately has the default behavior of executing befores/afters in the main thread while executing the actual test method in a separate thread... Within Spring, that behavior was retained for JUnit-specified timeouts, while things are all executing in the main thread in case of a Spring-specified timeout (
@Timed, which you could use as a workaround for the time being).As of Spring 2.5.3, SpringJUnit4ClassRunner always executes befores/afters in the same thread as the test method. As a consequence, JUnit-specified timeouts will work fine in combination with Spring transactions now. Note that JUnit-specified timeouts still differ from Spring-specific timeouts in that the former execute in a separate thread while the latter simply execute in the main thread (like regular tests).
Juergen
spring-projects-issues commentedon Mar 26, 2008
Juergen Hoeller commented
This will be available in tonight's 2.5.3 snapshot (http://static.springframework.org/downloads/nightly/snapshot-download.php?project=SPR). Please give it a try and let me know whether it works for you...
Juergen
spring-projects-issues commentedon Mar 26, 2008
Juergen Hoeller commented
This limitation is actually documented in the reference documentation, hence this is rather an improvement than a bug. The documentation suggests to use Spring's
@Timedinstead.There's also one important change in semantics here: Befores/afters are now part of the JUnit-specified timeout whereas previously only the actual test method execution counted for the timeout. This should be ok for all typical use cases but it's worth noting...
Juergen
spring-projects-issues commentedon Mar 26, 2008
Juergen Hoeller commented
There's unfortunately a concurrency issue hiding in TransactionalTestExecutionListener which previously didn't expect to be called for interleaved transactions from different threads (which may happen now that JUnit-style timeouts are supported for transactions as well). This will impact tests that fail because of a timeout. I'm working on revising TransactionalTestExecutionListener accordingly.
Juergen
spring-projects-issues commentedon Jun 25, 2014
Sam Brannen commented
Reopened for format code.