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 6028353
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:43:39+00:00 2026-05-23T04:43:39+00:00

I try to set up a Junit test case for my dao layer. However,

  • 0

I try to set up a Junit test case for my dao layer.
However, I do NOT want the test data to be actually persisted to the DB.

So I thought I should do it transactionally and rollback after every test.
This leaves me with the following datasource setup:

<bean id="dataSource"
    class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"
    p:driverClass="org.postgresql.Driver"
    p:jdbcUrl="jdbc:postgresql://***"
    p:user="***"
    p:password="***/>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
      p:dataSource-ref="dataSource"
      p:packagesToScan="***"
      p:hibernateProperties-ref="hibernateProps" />

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>

<tx:annotation-driven transaction-manager="transactionManager"/>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"
      p:dataSource-ref="dataSource"
      p:sessionFactory-ref="sessionFactory"/>

The to-be-tested dao class is set up as follows:

@Repository
@Transactional
@SuppressWarnings("unchecked")
public class BallotBoxRepositoryHibernateImpl implements BallotBoxRepository {

@Autowired
private SessionFactory sessionFactory;

  @Override
  public void saveVote(DaoObject v) {
    Session sess = sessionFactory.openSession();
    sess.beginTransaction();
    sess.save(v);
    sess.getTransaction().commit();
    sess.close();
  }
[...]
}

The actual persisting job does work nicely. However, the intended Rollback is never made:

INFO main transaction.TransactionalTestExecutionListener:292 - Rolled back transaction after test execution for test context [...]

The TransactionalTextExecutionListener is defined as follows:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:/spring/applicationContext.xml"})
@TestExecutionListeners({WebTestExecutionerListener.class, DependencyInjectionTestExecutionListener.class,
TransactionalTestExecutionListener.class})
@TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true)
@Transactional
public class DaoTest { ... }

I’m using an autowired member to access my dao methods:

@Autowired
private BallotBoxRepository repo;

TL;DR

JUnit test case is persisting test data even though it’s stating that a rollback has been done.

  • 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-05-23T04:43:40+00:00Added an answer on May 23, 2026 at 4:43 am

    In your case Spring-based transaction management doesn’t work because you manage Hibernate transactions manually.

    In order to use Spring-managed transactions you should do the following:

    @Override   
    public void saveVote(DaoObject v) {
        sessionFactory.getCurrentSession().save(v);
    } 
    

    See also:

    • 13.3 Hibernate
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple test set up in JUnit, along the lines of public
I used the code to try set the label text but its not working.
I try to set the anchorPoint property, in order to rotate a view by
I try to set an attribute in a XML node using MSXML. IXMLDOMElement alone
While we try to set up as many unit tests as time allows for
When I try to set the z variable in the code below, I get
Every time try to set the value of any variable in my model object,
I have try to set icon for my App in the xCode. In the
I have a WebBrowser control and try to set onclick and href attributes on
Usually, I try to set my folders to have permission 775, but some web-hosting

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.