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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:40:26+00:00 2026-05-15T03:40:26+00:00

I am testing my Hibernate DAOs with Spring and JUnit. I would like each

  • 0

I am testing my Hibernate DAOs with Spring and JUnit.

I would like each test method to start with a pre-populated DB, i.e. the Java objects have been saved in the DB, in a Hibernate transaction that has already been committed. How can I do this?

With @After and @Before, methods execute in the same Hibernate transaction as the methods decorated with @Test and @Transactional (first level cache may not be flushed by the time the real test method starts). @BeforeTransaction and @AfterTransaction apparently cannot work with Hibernate because they don’t create transactions even if the method is annotated with @Transactional in addition to @Before/AfterTransaction.

Any suggestion?

  • 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-15T03:40:27+00:00Added an answer on May 15, 2026 at 3:40 am

    One way could be to externalize your intialization logic to an external service with transactional methods which are executed from your @BeforeTransaction and @AfterTransaction annotated methods in the test class.

    Another benefit of this approach is the reusability of initialization code across tests.

    You could for example use the SpringJunit4ClassRunner like described here like this:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"testContext.xml","services.xml"})
    public class MyServiceTest {
    
        @Autowired    
        private TestDataService testDataService;
    
        @Before
        public void setUp(){
            testDataService.addTestData();
        }
    
        @Test
        public void testSomething() throws Exception {
             // ...
        }
    }
    
    
    public interface TestDataService {
    
        void addTestData();
    
    }
    
    public class TestDataServiceImpl implements TestDataService {
    
        @Transactional
        public void addTestData(){
            // TODO
        }
    
    }
    

    That’s something we do in our Spring based projects.

    Make sure the transactional configuration is correct. If you want to avoid the class/interface separation, then set proxy-target-class to true in the element.

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

Sidebar

Related Questions

We're running a java/hibernate app going against ORACLE 10g in TESTING. Once in a
I'm storing a simple java.util.date in an Oracle XE database via hibernate. When testing
Testing: return request.getCookies() == null; is not an appropriate way test. Is there another
Unit testing is, roughly speaking, testing bits of your code in isolation with test
I use Hibernate to generate my database automatically for testing, and I have some
I've got a hibernate-based application which uses DBUnit for unit testing. We have an
I am using NHibernate and Rhinomocks and having trouble testing what I want. I
Unit testing and ASP.NET web applications are an ambiguous point in my group. More
For testing purposes I have to generate a file of a certain size (to
Unit testing sounds great to me, but I'm not sure I should spend any

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.