Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8229953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:58:19+00:00 2026-06-07T16:58:19+00:00

I have a Spring 3.1 MVC + Hibernate 3.6 project with its junit4 test

  • 0

I have a Spring 3.1 MVC + Hibernate 3.6 project with its junit4 test suit. My problem is that there is no transaction starting in my test cases, even thought I added a @Transactional.

My test case calls a controller and a dao. In the controller, a transaction is started anyway, so it does not complain. In the dao, I added a @Transactional(propagation = Propagation.MANDATORY) to be sure it will take the test’s transaction. And currently it raises an IllegalTransactionStateException, which I guess it means there is no current transaction.

I tried to create programmaticaly an transaction and it does work, which means the AOP proxy to get the dao service is not the cause of the problem. However I want to create a transaction with the @Transactional annotation.

here’s my dao:

// ...imports...

@Repository("taskDao")
@Transactional(propagation = Propagation.MANDATORY)
public class TaskHome implements TaskDao {

    private static final Log log = LogFactory.getLog(TaskHome.class);

    private SessionFactory sessionFactory;

    @Autowired
    public TaskHome(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    public Task findById(int id) {
        log.debug("getting Task instance with id: " + id);
        try {
            Task instance = (Task) this.sessionFactory.getCurrentSession().get(
                    Task.class, id); // exception raised here!
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
                log.debug("get successful, instance found");
            }
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    ...
}

Here’s my test case:

// ...imports...

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({ "/test-config.xml", "/applicationContext.xml" })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class TestTaskController {

    private static ClassPathXmlApplicationContext context;
    private static TaskDao taskDao;

    @BeforeClass
    public static void initHibernate() throws Exception {
        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        taskDao = context.getBean("taskDao", TaskDao.class);
    }

    @Test
    public void testOnSubmit() {

        // expects an existing default transaction here

        Task task = taskDao.findById(1); // fails already here

        // ... calls the controller and does some tests. 

    }

}

I searched in all Spring’s documentation and googled it in any way I could imagine, but I don’t see why the transaction is not started.
Any help is very welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T16:58:20+00:00Added an answer on June 7, 2026 at 4:58 pm

    When using @RunWith(SpringJUnit4ClassRunner.class) you should obtain beans from the application context created by SpringJUnit4ClassRunner rather than from your own one.

    In your case things go wrong because @Transactional on the unit test creates a transaction in the application context managed by SpringJUnit4ClassRunner, but you call methods on the beans obtained from the application context created manually.

    So, remove your @BeforeClass method and obtain TaskDao via autowiring:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration({ "/test-config.xml", "/applicationContext.xml" })  
    @TransactionConfiguration(defaultRollback = true)
    @Transactional  
    public class TestTaskController {
        @Autowired
        private TaskDao taskDao; 
    
        ...
    }
    

    See also:

    • 9.3.5 Spring TestContext Framework
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a spring web app that uses Hibernate and Velocity. It's an MVC
I have a spring mvc project and am trying to test out a rest
I have Hibernate Transaction manager configured inside Spring MVC controller. <bean id=dataSource class=org.apache.commons.dbcp.BasicDataSource destroy-method=close>
I have a Mavenized Spring MVC project that deploys locally or remotely (passing all
I have an application built on Spring MVC that uses Hibernate for all of
I have Spring MVC project. I am using HSQL database and Hibernate. I have
I have some web-project (Spring MVC + Hibernate). For hibernate objects I am using
I have a spring mvc application that I am using Hibernate with. I am
I have a spring mvc application that I have broken up into separate maven
I have a localized spring mvc based web application, that has an externalized messages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.