Skip to content

Support declarative SQL script execution in the TestContext framework [SPR-7655] #12311

@spring-projects-issues

Description

@spring-projects-issues

omar elmandour opened SPR-7655 and commented

Overview

AbstractTransactionalJUnit4SpringContextTests and AbstractTransactionalTestNGSpringContextTests provide the ability to manually execute a specific SQL script for the current test via their protected executeSqlScript() methods. For more control over the configuration of such scripts, one can optionally use either ResourceDatabasePopulator or ScriptUtils directly.

The above provide several options for programmatically executing scripts; however, it would be nice if the Spring TestContext Framework provided declarative support via an annotation and an associated TestExecutionListener.


Deliverables

  1. The semantics for locating SQL script resources should be consistent with @ContextConfiguration's semantics for locating XML configuration files.
  2. There needs to be a mechanism for specifying which DataSource and PlatformTransactionManager to use from the test's ApplicationContext, including default conventions consistent with TransactionalTestExecutionListener and @TransactionConfiguration
  3. The annotation can be applied at either the class or method level with method-level overrides.
  4. The annotation can be used as a meta-annotation.
  5. The annotation must support all of the configuration options currently supported by ResourceDatabasePopulator - for example, encoding, statement separator, comment prefix, block comment delimiters, error handling flags, etc.
  6. The annotation and listener must support execution phases for scripts, for example "before test" and "after test".
  7. The listener should execute the named scripts within the current test's transaction if present, outside of the current test's transaction if present, or always in a new transaction, depending on the value of a boolean flag in the annotation.
  8. The listener should delegate to ResourceDatabasePopulator#execute to actually execute the scripts.
  9. The listener should be registered by default in abstract base classes as well as in TestContextBootstrapper implementations.

Proposed Annotation Names

  • @SqlGroup as a container with nested, repeatable @Sql entries
  • @ExecuteSql as a container with nested, repeatable @SqlScripts entries
  • @DatabaseInitializers as a container with nested, repeatable @DatabaseInitializer entries

Code Examples

Defaults
@Test
// Detects default SQL script location
@Sql
public void example() { /* ... */ }
Single Script
@Test
@Sql("data.sql")
public void example() { /* ... */ }
Multiple Scripts
@Test
@Sql({"schema.sql", "data.sql"})
public void example() { /* ... */ }
Multiple Annotations with Container
@Test
@SqlGroup({
	@Sql("schema.sql"),
	@Sql(
		scripts = "data.sql",
		commentPrefix = "//",
		separator = "@@"
	)
})
public void example() { /* ... */ }
Multiple Annotations without Container (Java 8)
@Test
@Sql("schema.sql"),
@Sql(
	scripts = "data.sql",
	commentPrefix = "//",
	separator = "@@"
)
})
public void example() { /* ... */ }

Affects: 3.0 GA

Issue Links:

Referenced from: commits 0c1249f, abdb010, 5fd6ebb, ae29f48

3 votes, 5 watchers

Metadata

Metadata

Assignees

Labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions