Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support table truncation teardown in the TestContext framework #14549

Closed
spring-projects-issues opened this issue Oct 23, 2012 · 1 comment
Closed
Labels
in: test Issues in the test module status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Oct 23, 2012

Adib Saikali opened SPR-9916 and commented

Overview

Currently the Spring TestContext Framework implements the Transaction rollback tear down for integration tests that touch the database. Another standard pattern for database tear down is the Table truncation tear down, discussed on page 661 in the xUnit Test Patterns Book.

There are many situations where the test needs to span multiple @Transactional service methods with many different propagation settings. Therefore running the whole integration test within a single @Transactional test method with transaction rollback is not enough. Right now this means doing some sort of cleanup of the work that the transactions did. The transaction cleanup code can be problematic to write and keep in sync with how the tests modify the database making test code brittle. Dropping and recreating the schema can be overkill because you have to rebuild some tables which are essentially immutable for the duration of the test, for example we might have tables with application usernames, passwords, preferences ... etc, that are essentially immutable for a vast majority of integration tests.

Proposal

The @Truncate annotation in the example below should instruct the TestContext framework to truncate all tables in the test schema other than test.users and test.roles.

Spring can scan the schema using JDBC metadata to find out all the table names and then can use the configuration information in the annotation to figure what tables to truncate or remove.

A class level @Truncate should truncate the tables after every test method, but placing @Truncate on a method can give fine-grained control over which tables to truncate after every test method.

One can play around with whether a single regex should be used to identify tables and schema or whether those should be separate settings.

@SpringJUnitConfig
@Truncate(schema = "test", tables = "*", keep = {"users", "roles"})
public class Test {
    // ...
}

An alternative to @Truncate could be a truncate() method in the abstract base transactional test classes for JUnit 4 and TestNG. The method could have a signature along the lines of the following.

public void truncate(String schema, String truncateTablesRegex, String keepTablesRegex)
@spring-projects-issues spring-projects-issues added in: test Issues in the test module type: enhancement A general enhancement labels Jan 11, 2019
@sbrannen sbrannen self-assigned this Jan 15, 2019
@sbrannen sbrannen changed the title Support table truncation teardown in the TestContext framework [SPR-9916] Support table truncation teardown in the TestContext framework Jan 19, 2024
@sbrannen sbrannen removed their assignment Jan 19, 2024
@sbrannen
Copy link
Member

Closing in favor of @Sql support in general as well as the recently added support for class-level execution phases for @Sql.

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Jan 19, 2024
@sbrannen sbrannen added the status: declined A suggestion or change that we don't feel we should currently apply label Jan 19, 2024
@sbrannen sbrannen removed this from the General Backlog milestone Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants