Skip to content

Transactional JUnit test with timeout does not create a transaction [SPR-4541] #9218

Closed

Description

@spring-projects-issues

Andy Kriger opened SPR-4541 and commented

Create a class for this source and run it as a unit test.

  • EXPECT: Both tests pass
  • ACTUAL: withTimeout fails
import org.junit.*;
import org.junit.runner.RunWith;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.hibernate.SessionFactory;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
 /* !!! YOU NEED A SPRING CONFIG THAT DEFINES A SESSIONFACTORY & TXMANAGER !!! */
})
@Transactional
public class SpringBug {

   @Autowired
   protected SessionFactory sessionFactory;
   
   @Test
   public void withoutTimeout() {
      sessionFactory.getCurrentSession();
   }

   @Test(timeout = 60000)
   public void withTimeout() {
      sessionFactory.getCurrentSession();
   }

}

Affects: 2.5.1

Issue Links:

Activity

  1. spring-projects-issues commented on Mar 10, 2008

    @spring-projects-issues
    CollaboratorAuthor

    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

  2. spring-projects-issues commented on Mar 26, 2008

    @spring-projects-issues
    CollaboratorAuthor

    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

  3. spring-projects-issues commented on Mar 26, 2008

    @spring-projects-issues
    CollaboratorAuthor
  4. spring-projects-issues commented on Mar 26, 2008

    @spring-projects-issues
    CollaboratorAuthor

    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 @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...

    Juergen

  5. spring-projects-issues commented on Mar 26, 2008

    @spring-projects-issues
    CollaboratorAuthor

    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

  6. spring-projects-issues commented on Jun 25, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Reopened for format code.

  7. added
    in: data
    type: enhancement
    on Jan 11, 2019
  8. added this to the 2.5.3 milestone 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: datatype: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions