@java.lang.annotation.Retention(value: RetentionPolicy.RUNTIME) @java.lang.annotation.Target(value: [ElementType.TYPE, ElementType.METHOD]) @interface DatabaseCleanup
Annotation to indicate that database tables should be truncated after test execution.
When applied at the class level, all test methods in the specification will have their database cleaned up after each test and after the spec completes. When applied at the method level, only the annotated test methods will trigger cleanup.
The optional value() attribute specifies which datasource bean names to clean and optionally which database type (cleaner) to use. Each entry can be:
"datasourceName" — clean the named datasource, auto-discovering the
appropriate DatabaseCleaner via
DatabaseCleaner.supports"datasourceName:databaseType" — clean the named datasource using the
DatabaseCleaner that declares the specified
DatabaseCleaner.databaseTypeIf the value is empty (the default), all datasources in the application context will be cleaned using auto-discovery.
The resolver() attribute allows users to specify a custom ApplicationContextResolver implementation for tests that store the org.springframework.context.ApplicationContext in a non-standard location. The default resolver, DefaultApplicationContextResolver, covers the common cases (Groovy property, field reflection, and Spring TestContextManager).
Example usage:
// Clean all datasources after every test (auto-discover cleaners)
@DatabaseCleanup
class MySpec extends Specification { ... }
// Clean only specific datasources (auto-discover cleaners)
@DatabaseCleanup(['dataSource', 'dataSource_secondary'])
class MySpec extends Specification { ... }
// Clean specific datasources with explicit cleaner types
@DatabaseCleanup(['dataSource:h2', 'dataSource_pg:postgresql'])
class MySpec extends Specification { ... }
// Mixed: some explicit, some auto-discovered
@DatabaseCleanup(['dataSource:h2', 'dataSource_other'])
class MySpec extends Specification { ... }
// Clean after a specific test method
@DatabaseCleanup
void "my test"() { ... }
// Use a custom ApplicationContext resolver
@DatabaseCleanup(resolver = MyCustomResolver)
class MySpec extends Specification { ... }
| Type Params | Return Type | Name and description |
|---|---|---|
|
abstract java.lang.Class<? extends ApplicationContextResolver> |
resolver()The ApplicationContextResolver implementation to use for resolving the org.springframework.context.ApplicationContext from the test instance. |
|
abstract java.lang.String[] |
value()The datasource entries to clean up. |
| Methods inherited from class | Name |
|---|---|
class java.lang.Object |
java.lang.Object#equals(java.lang.Object), java.lang.Object#getClass(), java.lang.Object#hashCode(), java.lang.Object#notify(), java.lang.Object#notifyAll(), java.lang.Object#toString(), java.lang.Object#wait(), java.lang.Object#wait(long), java.lang.Object#wait(long, int) |
The ApplicationContextResolver implementation to use for resolving the org.springframework.context.ApplicationContext from the test instance.
The default implementation, DefaultApplicationContextResolver, covers the common cases: Groovy property access, field reflection, and Spring TestContextManager. Override this to provide a custom resolution strategy for tests that store the context in a non-standard way.
The datasource entries to clean up. Each entry can be a plain datasource bean name
(e.g., "dataSource") or a datasource-to-type mapping
(e.g., "dataSource:h2"). If empty (the default), all data sources found
in the application context will be cleaned using auto-discovery.