Skip to content

Commit ccdf04e

Browse files
committed
Document @SqlMergeMode support in reference manual
See spring-projectsgh-1835
1 parent 89571ea commit ccdf04e

File tree

1 file changed

+80
-9
lines changed

1 file changed

+80
-9
lines changed

src/docs/asciidoc/testing.adoc

Lines changed: 80 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ Spring's testing annotations include the following:
419419
* <<spring-testing-annotation-aftertransaction>>
420420
* <<spring-testing-annotation-sql>>
421421
* <<spring-testing-annotation-sqlconfig>>
422+
* <<spring-testing-annotation-sqlmergemode>>
422423
* <<spring-testing-annotation-sqlgroup>>
423424

424425
[[spring-testing-annotation-bootstrapwith]]
@@ -968,9 +969,9 @@ it:
968969
----
969970
<1> Run two scripts for this test.
970971

971-
972972
See <<testcontext-executing-sql-declaratively>> for further details.
973973

974+
974975
[[spring-testing-annotation-sqlconfig]]
975976
===== `@SqlConfig`
976977

@@ -992,6 +993,56 @@ configured with the `@Sql` annotation. The following example shows how to use it
992993
<1> Set the comment prefix and the separator in SQL scripts.
993994

994995

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+
9951046
[[spring-testing-annotation-sqlgroup]]
9961047
===== `@SqlGroup`
9971048

@@ -1411,6 +1462,7 @@ You can use each of the following as a meta-annotation in conjunction with the
14111462
* `@Rollback`
14121463
* `@Sql`
14131464
* `@SqlConfig`
1465+
* `@SqlMergeMode`
14141466
* `@SqlGroup`
14151467
* `@Repeat` _(only supported on JUnit 4)_
14161468
* `@Timed` _(only supported on JUnit 4)_
@@ -3997,11 +4049,16 @@ various `executeSqlScript(..)` methods for further details.
39974049
In addition to the aforementioned mechanisms for running SQL scripts programmatically,
39984050
you can declaratively configure SQL scripts in the Spring TestContext Framework.
39994051
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.
40044055

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]]
40054062
====== Path Resource Semantics
40064063

40074064
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:
40344091
}
40354092
----
40364093

4094+
[[testcontext-executing-sql-declaratively-script-detection]]
40374095
====== Default Script Detection
40384096

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
40414099
`IllegalStateException` is thrown.
40424100

40434101
* 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
40464104
defined in the class `com.example.MyTest`, the corresponding default script is
40474105
`classpath:com/example/MyTest.testMethod.sql`.
40484106

4107+
[[testcontext-executing-sql-declaratively-multiple-annotations]]
40494108
====== Declaring Multiple `@Sql` Sets
40504109

40514110
If you need to configure multiple sets of SQL scripts for a given test class or test
@@ -4088,6 +4147,7 @@ Java 7.
40884147
}
40894148
----
40904149

4150+
[[testcontext-executing-sql-declaratively-script-execution-phases]]
40914151
====== Script Execution Phases
40924152

40934153
By default, SQL scripts are executed before the corresponding test method. However, if
@@ -4117,6 +4177,7 @@ following example shows:
41174177
Note that `ISOLATED` and `AFTER_TEST_METHOD` are statically imported from
41184178
`Sql.TransactionMode` and `Sql.ExecutionPhase`, respectively.
41194179

4180+
[[testcontext-executing-sql-declaratively-script-configuration]]
41204181
====== Script Configuration with `@SqlConfig`
41214182

41224183
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
41424203
individual attributes in {api-spring-framework}/test/context/jdbc/Sql.html[`@Sql`] and
41434204
{api-spring-framework}/test/context/jdbc/SqlConfig.html[`@SqlConfig`] for details.
41444205

4145-
4146-
41474206
[[testcontext-executing-sql-declaratively-tx]]
41484207
*Transaction management for `@Sql`*
41494208

@@ -4208,6 +4267,18 @@ run, since any changes made to the database (either within the test method or wi
42084267
`TransactionalTestExecutionListener` (see <<testcontext-tx,transaction management>> for
42094268
details).
42104269

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+
42114282

42124283
[[testcontext-parallel-test-execution]]
42134284
==== Parallel Test Execution

0 commit comments

Comments
 (0)