org.springframework.test.context.jdbc.Sql Maven / Gradle / Ivy
Show all versions of spring-test Show documentation
/*
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.context.jdbc;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.core.annotation.AliasFor;
/**
* {@code @Sql} is used to annotate a test class or test method to configure
* SQL {@link #scripts} and {@link #statements} to be executed against a given
* database during integration tests.
*
* Method-level declarations override class-level declarations by default,
* but this behavior can be configured via {@link SqlMergeMode @SqlMergeMode}.
* However, this does not apply to class-level declarations configured for the
* {@link ExecutionPhase#BEFORE_TEST_CLASS BEFORE_TEST_CLASS} or
* {@link ExecutionPhase#AFTER_TEST_CLASS AFTER_TEST_CLASS} execution phase. Such
* declarations cannot be overridden, and the corresponding scripts and statements
* will be executed once per class in addition to any method-level scripts and
* statements.
*
*
Script execution is performed by the {@link SqlScriptsTestExecutionListener},
* which is enabled by default.
*
*
The configuration options provided by this annotation and
* {@link SqlConfig @SqlConfig} are equivalent to those supported by
* {@link org.springframework.jdbc.datasource.init.ScriptUtils ScriptUtils} and
* {@link org.springframework.jdbc.datasource.init.ResourceDatabasePopulator ResourceDatabasePopulator}
* but are a superset of those provided by the {@code }
* XML namespace element. Consult the javadocs of individual attributes in this
* annotation and {@link SqlConfig @SqlConfig} for details.
*
*
{@code @Sql} can be used as a {@linkplain Repeatable repeatable}
* annotation. Otherwise, {@link SqlGroup @SqlGroup} can be used as an explicit
* container for declaring multiple instances of {@code @Sql}.
*
*
This annotation will be inherited from an enclosing test class by default. See
* {@link org.springframework.test.context.NestedTestConfiguration @NestedTestConfiguration}
* for details. This annotation may also be used as a meta-annotation to
* create custom composed annotations with attribute overrides.
*
*
If you want to see which SQL scripts are being executed, set the
* {@code org.springframework.test.context.jdbc} logging category to {@code DEBUG}.
* If you want to see which SQL statements are being executed, set the
* {@code org.springframework.jdbc.datasource.init} logging category to {@code DEBUG}.
*
*
Use of this annotation requires the {@code spring-jdbc} and {@code spring-tx}
* modules as well as their transitive dependencies to be present on the classpath.
*
* @author Sam Brannen
* @author Andreas Ahlenstorf
* @since 4.1
* @see SqlConfig
* @see SqlMergeMode
* @see SqlGroup
* @see SqlScriptsTestExecutionListener
* @see org.springframework.transaction.annotation.Transactional
* @see org.springframework.test.context.transaction.TransactionalTestExecutionListener
* @see org.springframework.jdbc.datasource.init.ResourceDatabasePopulator
* @see org.springframework.jdbc.datasource.init.ScriptUtils
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Repeatable(SqlGroup.class)
public @interface Sql {
/**
* Alias for {@link #scripts}.
*
This attribute may not be used in conjunction with
* {@link #scripts}, but it may be used instead of {@link #scripts}.
* @see #scripts
* @see #statements
*/
@AliasFor("scripts")
String[] value() default {};
/**
* The paths to the SQL scripts to execute.
*
This attribute may not be used in conjunction with
* {@link #value}, but it may be used instead of {@link #value}. Similarly,
* this attribute may be used in conjunction with or instead of
* {@link #statements}.
*
Path Resource Semantics
* Each path will be interpreted as a Spring
* {@link org.springframework.core.io.Resource Resource}. A plain path
* — for example, {@code "schema.sql"} — will be treated as a
* classpath resource that is relative to the package in which the
* test class is defined. A path starting with a slash will be treated as an
* absolute classpath resource, for example:
* {@code "/org/example/schema.sql"}. A path which references a
* URL (e.g., a path prefixed with
* {@link org.springframework.util.ResourceUtils#CLASSPATH_URL_PREFIX classpath:},
* {@link org.springframework.util.ResourceUtils#FILE_URL_PREFIX file:},
* {@code http:}, etc.) will be loaded using the specified resource protocol.
*
Default Script Detection
* If no SQL scripts or {@link #statements} are specified, an attempt will
* be made to detect a default script depending on where this
* annotation is declared. If a default cannot be detected, an
* {@link IllegalStateException} will be thrown.
*
* - class-level declaration: if the annotated test class
* is {@code com.example.MyTest}, the corresponding default script is
* {@code "classpath:com/example/MyTest.sql"}.
* - method-level declaration: if the annotated test
* method is named {@code testMethod()} and is defined in the class
* {@code com.example.MyTest}, the corresponding default script is
* {@code "classpath:com/example/MyTest.testMethod.sql"}.
*
* @see #value
* @see #statements
*/
@AliasFor("value")
String[] scripts() default {};
/**
* Inlined SQL statements to execute.
* This attribute may be used in conjunction with or instead of
* {@link #scripts}.
*
Ordering
* Statements declared via this attribute will be executed after
* statements loaded from resource {@link #scripts}. If you wish to have
* inlined statements executed before scripts, simply declare multiple
* instances of {@code @Sql} on the same class or method.
* @since 4.2
* @see #scripts
*/
String[] statements() default {};
/**
* When the SQL scripts and statements should be executed.
*
Defaults to {@link ExecutionPhase#BEFORE_TEST_METHOD BEFORE_TEST_METHOD}.
*/
ExecutionPhase executionPhase() default ExecutionPhase.BEFORE_TEST_METHOD;
/**
* Local configuration for the SQL scripts and statements declared within
* this {@code @Sql} annotation.
*
See the class-level javadocs for {@link SqlConfig} for explanations of
* local vs. global configuration, inheritance, overrides, etc.
*
Defaults to an empty {@link SqlConfig @SqlConfig} instance.
*/
SqlConfig config() default @SqlConfig;
/**
* Enumeration of phases that dictate when SQL scripts are executed.
*/
enum ExecutionPhase {
/**
* The configured SQL scripts and statements will be executed once per
* test class before any test method is run.
*
Specifically, the configured SQL scripts and statements will be
* executed prior to any before class lifecycle methods of a
* particular testing framework — for example, methods annotated
* with JUnit Jupiter's {@link org.junit.jupiter.api.BeforeAll @BeforeAll}
* annotation.
*
NOTE: Configuring {@code BEFORE_TEST_CLASS} as the execution phase
* causes the test's {@code ApplicationContext} to be eagerly loaded
* during test class initialization which can potentially result in
* undesired side effects. For example,
* {@link org.springframework.test.context.DynamicPropertySource @DynamicPropertySource}
* methods will be invoked before {@code @BeforeAll} methods when using
* {@code BEFORE_TEST_CLASS}.
* @since 6.1
* @see #AFTER_TEST_CLASS
* @see #BEFORE_TEST_METHOD
* @see #AFTER_TEST_METHOD
*/
BEFORE_TEST_CLASS,
/**
* The configured SQL scripts and statements will be executed once per
* test class after all test methods have run.
*
Specifically, the configured SQL scripts and statements will be
* executed after any after class lifecycle methods of a
* particular testing framework — for example, methods annotated
* with JUnit Jupiter's {@link org.junit.jupiter.api.AfterAll @AfterAll}
* annotation.
* @since 6.1
* @see #BEFORE_TEST_CLASS
* @see #BEFORE_TEST_METHOD
* @see #AFTER_TEST_METHOD
*/
AFTER_TEST_CLASS,
/**
* The configured SQL scripts and statements will be executed
* before the corresponding test method.
*
Specifically, the configured SQL scripts and statements will be
* executed prior to any before test lifecycle methods of a
* particular testing framework — for example, methods annotated
* with JUnit Jupiter's {@link org.junit.jupiter.api.BeforeEach @BeforeEach}
* annotation.
* @see #BEFORE_TEST_CLASS
* @see #AFTER_TEST_CLASS
* @see #AFTER_TEST_METHOD
*/
BEFORE_TEST_METHOD,
/**
* The configured SQL scripts and statements will be executed
* after the corresponding test method.
*
Specifically, the configured SQL scripts and statements will be
* executed after any after test lifecycle methods of a
* particular testing framework — for example, methods annotated
* with JUnit Jupiter's {@link org.junit.jupiter.api.AfterEach @AfterEach}
* annotation.
* @see #BEFORE_TEST_CLASS
* @see #AFTER_TEST_CLASS
* @see #BEFORE_TEST_METHOD
*/
AFTER_TEST_METHOD
}
}