|
|
@ -179,12 +179,11 @@ script, depending on where `@Sql` is declared. If a default cannot be detected, |
|
|
|
|
|
|
|
|
|
|
|
If you need to configure multiple sets of SQL scripts for a given test class or test |
|
|
|
If you need to configure multiple sets of SQL scripts for a given test class or test |
|
|
|
method but with different syntax configuration, different error handling rules, or |
|
|
|
method but with different syntax configuration, different error handling rules, or |
|
|
|
different execution phases per set, you can declare multiple instances of `@Sql`. With |
|
|
|
different execution phases per set, you can declare multiple instances of `@Sql`. You can |
|
|
|
Java 8, you can use `@Sql` as a repeatable annotation. Otherwise, you can use the |
|
|
|
either use `@Sql` as a repeatable annotation, or you can use the `@SqlGroup` annotation |
|
|
|
`@SqlGroup` annotation as an explicit container for declaring multiple instances of |
|
|
|
as an explicit container for declaring multiple instances of `@Sql`. |
|
|
|
`@Sql`. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The following example shows how to use `@Sql` as a repeatable annotation with Java 8: |
|
|
|
The following example shows how to use `@Sql` as a repeatable annotation: |
|
|
|
|
|
|
|
|
|
|
|
[tabs] |
|
|
|
[tabs] |
|
|
|
====== |
|
|
|
====== |
|
|
@ -204,7 +203,12 @@ Kotlin:: |
|
|
|
+ |
|
|
|
+ |
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
// Repeatable annotations with non-SOURCE retention are not yet supported by Kotlin |
|
|
|
@Test |
|
|
|
|
|
|
|
@Sql("/test-schema.sql", config = SqlConfig(commentPrefix = "`")) |
|
|
|
|
|
|
|
@Sql("/test-user-data.sql") |
|
|
|
|
|
|
|
fun userTest() { |
|
|
|
|
|
|
|
// run code that uses the test schema and test data |
|
|
|
|
|
|
|
} |
|
|
|
---- |
|
|
|
---- |
|
|
|
====== |
|
|
|
====== |
|
|
|
|
|
|
|
|
|
|
@ -212,9 +216,8 @@ In the scenario presented in the preceding example, the `test-schema.sql` script |
|
|
|
different syntax for single-line comments. |
|
|
|
different syntax for single-line comments. |
|
|
|
|
|
|
|
|
|
|
|
The following example is identical to the preceding example, except that the `@Sql` |
|
|
|
The following example is identical to the preceding example, except that the `@Sql` |
|
|
|
declarations are grouped together within `@SqlGroup`. With Java 8 and above, the use of |
|
|
|
declarations are grouped together within `@SqlGroup`. The use of `@SqlGroup` is optional, |
|
|
|
`@SqlGroup` is optional, but you may need to use `@SqlGroup` for compatibility with |
|
|
|
but you may need to use `@SqlGroup` for compatibility with other JVM languages. |
|
|
|
other JVM languages such as Kotlin. |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[tabs] |
|
|
|
[tabs] |
|
|
|
====== |
|
|
|
====== |
|
|
@ -239,7 +242,8 @@ Kotlin:: |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@SqlGroup( |
|
|
|
@SqlGroup( |
|
|
|
Sql("/test-schema.sql", config = SqlConfig(commentPrefix = "`")), |
|
|
|
Sql("/test-schema.sql", config = SqlConfig(commentPrefix = "`")), |
|
|
|
Sql("/test-user-data.sql")) |
|
|
|
Sql("/test-user-data.sql") |
|
|
|
|
|
|
|
) |
|
|
|
fun userTest() { |
|
|
|
fun userTest() { |
|
|
|
// Run code that uses the test schema and test data |
|
|
|
// Run code that uses the test schema and test data |
|
|
|
} |
|
|
|
} |
|
|
@ -249,10 +253,10 @@ Kotlin:: |
|
|
|
[[testcontext-executing-sql-declaratively-script-execution-phases]] |
|
|
|
[[testcontext-executing-sql-declaratively-script-execution-phases]] |
|
|
|
=== Script Execution Phases |
|
|
|
=== Script Execution Phases |
|
|
|
|
|
|
|
|
|
|
|
By default, SQL scripts are run before the corresponding test method. However, if |
|
|
|
By default, SQL scripts are run before the corresponding test method. However, if you |
|
|
|
you need to run a particular set of scripts after the test method (for example, to clean |
|
|
|
need to run a particular set of scripts after the test method (for example, to clean up |
|
|
|
up database state), you can use the `executionPhase` attribute in `@Sql`, as the |
|
|
|
database state), you can set the `executionPhase` attribute in `@Sql` to |
|
|
|
following example shows: |
|
|
|
`AFTER_TEST_METHOD`, as the following example shows: |
|
|
|
|
|
|
|
|
|
|
|
[tabs] |
|
|
|
[tabs] |
|
|
|
====== |
|
|
|
====== |
|
|
@ -281,12 +285,11 @@ Kotlin:: |
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
|
|
|
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] |
|
|
|
---- |
|
|
|
---- |
|
|
|
@Test |
|
|
|
@Test |
|
|
|
@SqlGroup( |
|
|
|
@Sql("create-test-data.sql", |
|
|
|
Sql("create-test-data.sql", |
|
|
|
config = SqlConfig(transactionMode = ISOLATED)) |
|
|
|
config = SqlConfig(transactionMode = ISOLATED)), |
|
|
|
@Sql("delete-test-data.sql", |
|
|
|
Sql("delete-test-data.sql", |
|
|
|
config = SqlConfig(transactionMode = ISOLATED), |
|
|
|
config = SqlConfig(transactionMode = ISOLATED), |
|
|
|
executionPhase = AFTER_TEST_METHOD) |
|
|
|
executionPhase = AFTER_TEST_METHOD)) |
|
|
|
|
|
|
|
fun userTest() { |
|
|
|
fun userTest() { |
|
|
|
// run code that needs the test data to be committed |
|
|
|
// run code that needs the test data to be committed |
|
|
|
// to the database outside of the test's transaction |
|
|
|
// to the database outside of the test's transaction |
|
|
@ -294,7 +297,7 @@ Kotlin:: |
|
|
|
---- |
|
|
|
---- |
|
|
|
====== |
|
|
|
====== |
|
|
|
|
|
|
|
|
|
|
|
Note that `ISOLATED` and `AFTER_TEST_METHOD` are statically imported from |
|
|
|
NOTE: `ISOLATED` and `AFTER_TEST_METHOD` are statically imported from |
|
|
|
`Sql.TransactionMode` and `Sql.ExecutionPhase`, respectively. |
|
|
|
`Sql.TransactionMode` and `Sql.ExecutionPhase`, respectively. |
|
|
|
|
|
|
|
|
|
|
|
[[testcontext-executing-sql-declaratively-script-configuration]] |
|
|
|
[[testcontext-executing-sql-declaratively-script-configuration]] |
|
|
|