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

  • Home
  • SEARCH
  • 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 6215213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:00:52+00:00 2026-05-24T07:00:52+00:00

In my project, I’ve used spring, jpa with PostgreSQL DB, I’ve lots of table

  • 0

In my project, I’ve used spring, jpa with PostgreSQL DB,
I’ve lots of table in DB and I need to have Unit testing of all of them.

Is there any framework which just rollback all the transactions after each test finished so every test will have fresh/same DB data to Test. And this way after all Test executions, data of DB schema would be as it is.

Any suggestion for this?

I’ve some idea of DBUnit but in that I need to write .xml files for every input data for every test and need to insert data in setup() and clear/remove data in tearDown(), but doesn’t seems better strategy to me.

Any suggestion is appreciated.
Thanks.

  • 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-24T07:00:53+00:00Added an answer on May 24, 2026 at 7:00 am

    Is there any framework which just rollback all the transactions after each test finished so every test will have fresh/same DB data to Test. And this way after all Test executions, data of DB schema would be as it is.

    From my other answer posted earlier in the day, yes, this is possible using DbUnit. (Based on your edit, you don’t need this; the subsequent section of my answer addresses why I use DbUnit, and when I wouldn’t use it).

    The following code snippet demonstrates how the setup of every test is performed:

    @Before
    public void setUp() throws Exception
    {
        logger.info("Performing the setup of test {}", testName.getMethodName());
        IDatabaseConnection connection = null;
        try
        {
            connection = getConnection();
            IDataSet dataSet = getDataSet();
            //The following line cleans up all DbUnit recognized tables and inserts and test data before every test.
            DatabaseOperation.CLEAN_INSERT.execute(connection, dataSet);
        }
        finally
        {
            // Closes the connection as the persistence layer gets it's connection from elsewhere
            connection.close();
        }
    }
    
    private IDatabaseConnection getConnection() throws Exception
    {
        @SuppressWarnings({ "rawtypes", "unused" })
        Class driverClass = Class.forName("org.apache.derby.jdbc.ClientDriver");
        Connection jdbcConnection = DriverManager.getConnection(jdbcURL, "XXX",
                "YYY");
        IDatabaseConnection databaseConnection = new DatabaseConnection(jdbcConnection);
        return databaseConnection;
    }
    
    private IDataSet getDataSet() throws Exception
    {
        ClassLoader classLoader = this.getClass().getClassLoader();
        return new FlatXmlDataSetBuilder().build(classLoader.getResourceAsStream("database-test-setup.xml"));
    }
    

    The database-test-setup.xml file contains the data that will be inserted into the database for every test. The use of DatabaseOperation.CLEAN_INSERT in the setup method ensures that all the tables specified in the file will be cleared (by a delete of all rows) followed by an insert of the specified data in the test data file.

    Avoiding DbUnit

    I use the above approach specifically to clear out sequences before the start of every test, as the application uses a JPA provider which updates the sequences in a separate transaction. If your application is not doing anything like that, then you can afford to simply start a transaction in your setup() method and issue a rollback on teardown after the test. If my application didn’t use sequences (and if I didn’t desire to reset them), then my setup routine would have been as simple as:

    @Before
    public void setUp() throws Exception
    {
        logger.info("Performing the setup of test {}", testName.getMethodName());
        // emf is created in the @BeforeClass annotated method
        em = emf.createEntityManager();
        // Starts the transaction before every test
        em.getTransaction.begin();
    }
    
    @After
    public void tearDown() throws Exception
    {
        logger.info("Performing the teardown of test {}", testName.getMethodName());
        if (em != null)
        {
            // Rolls back the transaction after every test
            em.getTransaction().rollback();
            em.close();
        }
    }
    

    Also, I use dbdeploy with Maven, but that is primarily for keeping the test database up to date with the versioned data model.

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

Sidebar

Related Questions

Project: ASP.NET 3.5 with C# I have this much :- A table which used
Project doesn't need any javascript libraries such as jQuery, Dojo, Prototype so there is
Project layout: /project_a /shared /project_b /shared /shared project_a and project_b both need to contain
My project is based on spring framework 2.5.4. And I try to add aspects
The project is ASP.NET 2.0, I have never been able to reproduce this myself,
Project Requirements: I need to build a web service that will receive chunks of
Project I'm working on uses jQuery. I have a series of Ajax calls being
Project Euler I have recently begun to solve some of the Project Euler riddles.
project is written on php. There is timestamp field in mysql it updates automatically.
Project is C#. So I have a bunch of multithreaded code that is designed

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.