@@ -419,6 +419,7 @@ Spring's testing annotations include the following:
419
419
* <<spring-testing-annotation-aftertransaction>>
420
420
* <<spring-testing-annotation-sql>>
421
421
* <<spring-testing-annotation-sqlconfig>>
422
+ * <<spring-testing-annotation-sqlmergemode>>
422
423
* <<spring-testing-annotation-sqlgroup>>
423
424
424
425
[[spring-testing-annotation-bootstrapwith]]
968
969
----
969
970
<1> Run two scripts for this test.
970
971
971
-
972
972
See <<testcontext-executing-sql-declaratively>> for further details.
973
973
974
+
974
975
[[spring-testing-annotation-sqlconfig]]
975
976
===== `@SqlConfig`
976
977
@@ -992,6 +993,56 @@ configured with the `@Sql` annotation. The following example shows how to use it
992
993
<1> Set the comment prefix and the separator in SQL scripts.
993
994
994
995
996
+ [[spring-testing-annotation-sqlmergemode]]
997
+ ===== `@SqlMergeMode`
998
+
999
+ `@SqlMergeMode` is used to annotate a test class or test method to configure whether
1000
+ method-level `@Sql` declarations are merged with class-level `@Sql` declarations. If
1001
+ `@SqlMergeMode` is not declared on a test class or test method, the `OVERRIDE` merge mode
1002
+ will be used by default. With the `OVERRIDE` mode, method-level `@Sql` declarations will
1003
+ effectively override class-level `@Sql` declarations.
1004
+
1005
+ Note that a method-level `@SqlMergeMode` declaration overrides a class-level declaration.
1006
+
1007
+ The following example shows how to use `@SqlMergeMode` at the class level.
1008
+
1009
+ [source,java,indent=0]
1010
+ [subs="verbatim,quotes"]
1011
+ ----
1012
+ @SpringJUnitConfig(TestConfig.class)
1013
+ @Sql("/test-schema.sql")
1014
+ @SqlMergeMode(MERGE) <1>
1015
+ class UserTests {
1016
+
1017
+ @Test
1018
+ @Sql("/user-test-data-001.sql")
1019
+ void standardUserProfile {
1020
+ // execute code that relies on test data set 001
1021
+ }
1022
+ }
1023
+ ----
1024
+ <1> Set the `@Sql` merge mode to `MERGE` for all test methods in the class.
1025
+
1026
+ The following example shows how to use `@SqlMergeMode` at the method level.
1027
+
1028
+ [source,java,indent=0]
1029
+ [subs="verbatim,quotes"]
1030
+ ----
1031
+ @SpringJUnitConfig(TestConfig.class)
1032
+ @Sql("/test-schema.sql")
1033
+ class UserTests {
1034
+
1035
+ @Test
1036
+ @Sql("/user-test-data-001.sql")
1037
+ @SqlMergeMode(MERGE) <1>
1038
+ void standardUserProfile {
1039
+ // execute code that relies on test data set 001
1040
+ }
1041
+ }
1042
+ ----
1043
+ <1> Set the `@Sql` merge mode to `MERGE` for a specific test method.
1044
+
1045
+
995
1046
[[spring-testing-annotation-sqlgroup]]
996
1047
===== `@SqlGroup`
997
1048
@@ -1411,6 +1462,7 @@ You can use each of the following as a meta-annotation in conjunction with the
1411
1462
* `@Rollback`
1412
1463
* `@Sql`
1413
1464
* `@SqlConfig`
1465
+ * `@SqlMergeMode`
1414
1466
* `@SqlGroup`
1415
1467
* `@Repeat` _(only supported on JUnit 4)_
1416
1468
* `@Timed` _(only supported on JUnit 4)_
@@ -3997,11 +4049,16 @@ various `executeSqlScript(..)` methods for further details.
3997
4049
In addition to the aforementioned mechanisms for running SQL scripts programmatically,
3998
4050
you can declaratively configure SQL scripts in the Spring TestContext Framework.
3999
4051
Specifically, you can declare the `@Sql` annotation on a test class or test method to
4000
- configure the resource paths to SQL scripts that should be run against a given database
4001
- before or after an integration test method. Note that method-level declarations override
4002
- class-level declarations and that support for `@Sql` is provided by the
4003
- `SqlScriptsTestExecutionListener`, which is enabled by default.
4052
+ configure individual SQL statements or the resource paths to SQL scripts that should be
4053
+ run against a given database before or after an integration test method. Support for
4054
+ `@Sql` is provided by the `SqlScriptsTestExecutionListener`, which is enabled by default.
4004
4055
4056
+ NOTE: Method-level `@Sql` declarations override class-level declarations by default. As
4057
+ of Spring Framework 5.2, however, this behavior may be configured per test class or per
4058
+ test method via `@SqlMergeMode`. See
4059
+ <<testcontext-executing-sql-declaratively-script-merging>> for further details.
4060
+
4061
+ [[testcontext-executing-sql-declaratively-script-resources]]
4005
4062
====== Path Resource Semantics
4006
4063
4007
4064
Each path is interpreted as a Spring `Resource`. A plain path (for example,
@@ -4034,10 +4091,11 @@ within a JUnit Jupiter based integration test class:
4034
4091
}
4035
4092
----
4036
4093
4094
+ [[testcontext-executing-sql-declaratively-script-detection]]
4037
4095
====== Default Script Detection
4038
4096
4039
- If no SQL scripts are specified, an attempt is made to detect a `default` script,
4040
- depending on where `@Sql` is declared. If a default cannot be detected, an
4097
+ If no SQL scripts or statements are specified, an attempt is made to detect a `default`
4098
+ script, depending on where `@Sql` is declared. If a default cannot be detected, an
4041
4099
`IllegalStateException` is thrown.
4042
4100
4043
4101
* Class-level declaration: If the annotated test class is `com.example.MyTest`, the
@@ -4046,6 +4104,7 @@ depending on where `@Sql` is declared. If a default cannot be detected, an
4046
4104
defined in the class `com.example.MyTest`, the corresponding default script is
4047
4105
`classpath:com/example/MyTest.testMethod.sql`.
4048
4106
4107
+ [[testcontext-executing-sql-declaratively-multiple-annotations]]
4049
4108
====== Declaring Multiple `@Sql` Sets
4050
4109
4051
4110
If you need to configure multiple sets of SQL scripts for a given test class or test
@@ -4088,6 +4147,7 @@ Java 7.
4088
4147
}
4089
4148
----
4090
4149
4150
+ [[testcontext-executing-sql-declaratively-script-execution-phases]]
4091
4151
====== Script Execution Phases
4092
4152
4093
4153
By default, SQL scripts are executed before the corresponding test method. However, if
@@ -4117,6 +4177,7 @@ following example shows:
4117
4177
Note that `ISOLATED` and `AFTER_TEST_METHOD` are statically imported from
4118
4178
`Sql.TransactionMode` and `Sql.ExecutionPhase`, respectively.
4119
4179
4180
+ [[testcontext-executing-sql-declaratively-script-configuration]]
4120
4181
====== Script Configuration with `@SqlConfig`
4121
4182
4122
4183
You can configure script parsing and error handling by using the `@SqlConfig` annotation.
@@ -4142,8 +4203,6 @@ provided by the `<jdbc:initialize-database/>` XML namespace element. See the jav
4142
4203
individual attributes in {api-spring-framework}/test/context/jdbc/Sql.html[`@Sql`] and
4143
4204
{api-spring-framework}/test/context/jdbc/SqlConfig.html[`@SqlConfig`] for details.
4144
4205
4145
-
4146
-
4147
4206
[[testcontext-executing-sql-declaratively-tx]]
4148
4207
*Transaction management for `@Sql`*
4149
4208
@@ -4208,6 +4267,18 @@ run, since any changes made to the database (either within the test method or wi
4208
4267
`TransactionalTestExecutionListener` (see <<testcontext-tx,transaction management>> for
4209
4268
details).
4210
4269
4270
+ [[testcontext-executing-sql-declaratively-script-merging]]
4271
+ ====== Merging and Overriding Configuration with `@SqlMergeMode`
4272
+
4273
+ As of Spring Framework 5.2, it is possible to merge method-level `@Sql` declarations with
4274
+ class-level declarations. For example, this allows you to provide the configuration for a
4275
+ database schema or some common test data once per test class and then provide additional,
4276
+ use case specific test data per test method. To enable `@Sql` merging, annotate either
4277
+ your test class or test method with `@SqlMergeMode(MERGE)`. To disable merging for a
4278
+ specific test method (or specific test subclass), you can switch back to the default mode
4279
+ via `@SqlMergeMode(OVERRIDE)`. Consult the <<spring-testing-annotation-sqlmergemode,
4280
+ `@SqlMergeMode` annotation documentation section>> for examples and further details.
4281
+
4211
4282
4212
4283
[[testcontext-parallel-test-execution]]
4213
4284
==== Parallel Test Execution
0 commit comments