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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:33:46+00:00 2026-05-28T14:33:46+00:00

Spring Test helpfully rolls back any changes made to the database within a test

  • 0

Spring Test helpfully rolls back any changes made to the database within a test method. This means that it is not necessary to take the time to delete/reload the test data before each test method.

But if you use the @BeforeClass Junit annotation, then that forces the data loader to be static. A question that is explored here: Why must jUnit's fixtureSetup be static?

If the data initialization method is static, so must the data connection methods and the data source..and on and on…forcing everything to be static…which won’t work. At which point, I ask – what good is Spring Test’s ability to rollback changes when you have to delete/reload the test data anyway for every test??!?!

  • 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-28T14:33:47+00:00Added an answer on May 28, 2026 at 2:33 pm

    One approach that works is to create a “data initialiser” class, add it to a test Spring application context that also has your data source, and wire this application context into your tests. This relies on the fact that Spring caches the application context between test invocations.

    For example, a test superclass:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:test-application-context.xml"})
    @Transactional
    public abstract class DataLoadingTest {
        @Autowired
        protected DatabaseInitialiser databaseInitialiser;
    }
    

    With test-application-context.xml:

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
        <bean id="dataSource" .../>
    
        <bean class="DatabaseInitialiser">
            <property name="dataSource" ref="dataSource"/>
        </bean>
    </beans>
    

    And

    public class DatabaseInitialiser extends JdbcDaoSupport {
        @PostConstruct
        public void load() {
            // Initialise your database here: create schema, use DBUnit to load data, etc.
        }
    }
    

    In this example:

    • all tests that rely on the database extend DataLoadingTest;
    • Spring initialises the application context upon first test invocation;
    • this calls DatabaseInitialiser.load(), via the @PostConstruct annotation;
    • Spring keeps the application context in a cache;
    • further test invocations wire in the DatabaseInitialiser from the application context, which is already cached;
    • tests are transactional, and roll back at the end to the initial data set.

    Likewise, DatabaseInitialiser can have a method annotated @PostDestroy to perform any rollback necessary at the end of the whole test run.

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

Sidebar

Related Questions

I have a method under test. Within its call stack, it calls a DAO
See the following Mock Test by using Spring/Spring-MVC public class OrderTest { // SimpleFormController
Is it possible to JUnit test if wiring by Spring is succesfully? I would
I'm currently converting a test class which extended the Spring class AbstractTransactionalSpringContextTests to Spring
I have a string of test like this: <customtag>hey</customtag> I want to use a
This code works: class Test { public: Test(string name) : _name(name) {}; bool operator()()
I have already read this post , but I am not sure know to
A RIA Domain service has this method... public virtual CmsDealer GetCmsDealer(string id) { return
As a mostly front-end developer, this is in the realm of computer science that
i have a small maven project with <artifactId>spring-core</artifactId> <artifactId>spring-test</artifactId> <artifactId>spring-beans</artifactId> <artifactId>spring-context</artifactId> <artifactId>spring-aop</artifactId> <artifactId>spring-context-support</artifactId> <artifactId>spring-tx</artifactId>

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.