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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:42:04+00:00 2026-05-18T00:42:04+00:00

I have no problem testing my DAO and services, but when I test INSERT

  • 0

I have no problem testing my DAO and services, but when I test INSERTs or UPDATEs I want to rollback the transaction and not effect my database.

I’m using @Transactional inside my services to manage transactions. I want to know, is it possible to know if a transaction will be fine, but rollback it to prevent altering database?

This is my Test:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:/META-INF/spring.cfg.xml")
@TransactionConfiguration(defaultRollback=true)
public class MyServiceTest extends AbstractJUnit38SpringContextTests  {
    @Autowired
    private MyService myService;

    @BeforeClass
    public static void setUpClass() throws Exception {
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @Test
    public void testInsert(){
        long id = myService.addPerson( "JUNIT" );
        assertNotNull( id );
        if( id < 1 ){
            fail();
        }
    }
}

The problem is that this test will fail because transaction was rollbacked, but the insert is OK!
If I remove @TransactionConfiguration(defaultRollback=true) then the test pass but a new record will be inserted into database.

@Test
@Transactional
@Rollback(true)
public void testInsert(){
    long id = myService.addPerson( "JUNIT" );
assertNotNull(id);
if( id < 1 ){
        fail();
    }
}

Now can test pass correctly, but rollback is ignored and the record is inserted into the database.
I have annotated the method addPerson() inside myService with @Transactional, obviously.
Why is the rollback being ignored?

  • 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-18T00:42:04+00:00Added an answer on May 18, 2026 at 12:42 am

    You need to extend transaction boundaries to the boundaries of your test method. You can do it by annotating your test method (or the whole test class) as @Transactional:

    @Test 
    @Transactional
    public void testInsert(){ 
        long id=myService.addPerson("JUNIT"); 
        assertNotNull(id); 
        if(id<1){ 
            fail(); 
        } 
    } 
    

    You can also use this approach to ensure that data was correctly written before rollback:

    @Autowired SessionFactory sf;
    
    @Test 
    @Transactional
    public void testInsert(){ 
        myService.addPerson("JUNIT"); 
        sf.getCurrentSession().flush();
        sf.getCurrentSession().doWork( ... check database state ... ); 
    } 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am toying a bit with Spring testing framework, but I have one problem.
I have problem concerning python packages and testing. I'm writing an application using wx
I have a problem executing a process from our testing server. On my localhost
I'm testing a new TeeChart HTML5/javascript and I have a problem with zoom. When
I have a problem with WCF. My testing code is pretty simple. I call
I'm working on the n-queens problem and am testing what I have so far
I have a very strange problem when I'm testing my application on device. I
I have quite a strange problem with PHP and Apache on my local testing
Here a small piece of code for testing and explain the problem. I have
I have a problem with links testing locally using Apache with Windows. The final

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.