You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
For more targeted logging, annotate individual methods instead of the entire repository. See the [SQL Logging](sql-logging.md) page for details.
252
252
253
-
### Use StatementCapture in Tests
253
+
### Use SqlCapture in Tests
254
254
255
-
The `StatementCapture` class from `storm-test` records all SQL statements generated during a block of code. This is useful for verifying that the correct queries are being generated:
255
+
The `SqlCapture` class from `storm-test` records all SQL statements generated during a block of code. This is useful for verifying that the correct queries are being generated:
Copy file name to clipboardExpand all lines: docs/testing.md
+26-26Lines changed: 26 additions & 26 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ Writing tests for database code can involve repetitive setup: creating a `DataSo
8
8
The module provides two categories of functionality:
9
9
10
10
1.**JUnit 5 integration** (`@StormTest`) for automatic database setup, script execution, and parameter injection.
11
-
2.**Statement capture** (`StatementCapture`) for recording and inspecting SQL statements generated during test execution. This component is framework-agnostic and works independently of JUnit.
11
+
2.**Statement capture** (`SqlCapture`) for recording and inspecting SQL statements generated during test execution. This component is framework-agnostic and works independently of JUnit.
12
12
13
13
---
14
14
@@ -46,7 +46,7 @@ The module uses H2 as its default in-memory database. To use H2, add it as a tes
46
46
47
47
JUnit 5 (`junit-jupiter-api`) is an optional dependency of `storm-test`. It is not pulled in transitively, so it does not appear on your classpath unless you add it yourself. Most projects already have JUnit Jupiter as a test dependency, in which case the `@StormTest` annotation and `StormExtension` are available automatically with no extra configuration.
48
48
49
-
If you only need `StatementCapture` and `CapturedStatement` (for example, in a project that uses TestNG, or for development-time debugging outside of any test framework), `storm-test` works without JUnit on the classpath. The JUnit-specific classes simply remain unused.
49
+
If you only need `SqlCapture` and `CapturedSql` (for example, in a project that uses TestNG, or for development-time debugging outside of any test framework), `storm-test` works without JUnit on the classpath. The JUnit-specific classes simply remain unused.
50
50
51
51
---
52
52
@@ -107,7 +107,7 @@ Test methods can declare parameters of the following types, and Storm will resol
|`StatementCapture`| A fresh capture instance for recording SQL statements (see below). |
110
+
|`SqlCapture`| A fresh capture instance for recording SQL statements (see below). |
111
111
| Any type with a static `of(DataSource)` factory method | An instance created via that factory method. This covers `ORMTemplate` and custom types that follow the same pattern. |
112
112
113
113
The factory method resolution also supports Kotlin companion objects. If a class has a `Companion` field with an `of(DataSource)` method, Storm will use it. This means `ORMTemplate` works seamlessly in both Kotlin and Java tests without any additional configuration.
@@ -195,11 +195,11 @@ class PostgresTest {
195
195
196
196
## Statement Capture
197
197
198
-
When testing database code, knowing _what_ SQL is executed is often as important as knowing _whether_ the operation succeeded. A test might pass because the correct rows were returned, but the underlying query could be inefficient, missing a filter, or using unexpected parameters. `StatementCapture` gives you visibility into the SQL that Storm generates, so you can write assertions not just on results, but on the queries themselves.
198
+
When testing database code, knowing _what_ SQL is executed is often as important as knowing _whether_ the operation succeeded. A test might pass because the correct rows were returned, but the underlying query could be inefficient, missing a filter, or using unexpected parameters. `SqlCapture` gives you visibility into the SQL that Storm generates, so you can write assertions not just on results, but on the queries themselves.
199
199
200
-
`StatementCapture` records every SQL statement generated during a block of code, along with its operation type (`SELECT`, `INSERT`, `UPDATE`, `DELETE`) and bound parameter values. It provides a high-level API designed for test assertions: count statements, filter by operation type, and inspect individual queries.
200
+
`SqlCapture` records every SQL statement generated during a block of code, along with its operation type (`SELECT`, `INSERT`, `UPDATE`, `DELETE`) and bound parameter values. It provides a high-level API designed for test assertions: count statements, filter by operation type, and inspect individual queries.
201
201
202
-
`StatementCapture` is framework-agnostic. It does not depend on JUnit and can be used with any test framework, or even outside of tests entirely (for example, in development-time debugging or diagnostics).
202
+
`SqlCapture` is framework-agnostic. It does not depend on JUnit and can be used with any test framework, or even outside of tests entirely (for example, in development-time debugging or diagnostics).
203
203
204
204
### Use Cases
205
205
@@ -209,7 +209,7 @@ When testing database code, knowing _what_ SQL is executed is often as important
209
209
210
210
**Inspecting SQL structure.** For custom queries or complex filter logic, you may want to verify that the generated SQL contains specific clauses (such as a `WHERE` condition or a `JOIN`) or that the correct parameters were bound. This is especially useful when testing query builder logic that constructs dynamic predicates.
211
211
212
-
**Debugging during development.** When a query does not return the expected results, wrapping the operation in a `StatementCapture` block lets you print the exact SQL and parameters without configuring logging or attaching a debugger.
212
+
**Debugging during development.** When a query does not return the expected results, wrapping the operation in a `SqlCapture` block lets you print the exact SQL and parameters without configuring logging or attaching a debugger.
213
213
214
214
### Basic Usage
215
215
@@ -219,7 +219,7 @@ Wrap any Storm operation in a `run`, `execute`, or `executeThrowing` call to cap
Operation op = stmt.operation(); // SELECT, INSERT, UPDATE, DELETE, or UNDEFINED
313
313
```
314
314
315
315
### Accumulation and Clearing
316
316
317
-
Statements accumulate across multiple `run`/`execute` calls on the same `StatementCapture` instance. This is useful when you want to measure the total SQL activity of a sequence of operations. Use `clear()` to reset between captures when you need to measure operations independently:
317
+
Statements accumulate across multiple `run`/`execute` calls on the same `SqlCapture` instance. This is useful when you want to measure the total SQL activity of a sequence of operations. Use `clear()` to reset between captures when you need to measure operations independently:
A count assertion is the simplest and most common use of `StatementCapture`. It protects against regressions where a code change inadvertently introduces extra queries:
330
+
A count assertion is the simplest and most common use of `SqlCapture`. It protects against regressions where a code change inadvertently introduces extra queries:
331
331
332
332
<TabsgroupId="language">
333
333
<TabItemvalue="kotlin"label="Kotlin"default>
334
334
335
335
```kotlin
336
336
@Test
337
-
fun`bulk insert should use single statement`(orm:ORMTemplate, capture:StatementCapture) {
337
+
fun`bulk insert should use single statement`(orm:ORMTemplate, capture:SqlCapture) {
338
338
val items =listOf(Item(name ="A"), Item(name ="B"), Item(name ="C"))
When using `@StormTest`, a fresh `StatementCapture` instance is automatically injected into each test method that declares it as a parameter. This means you do not need to create one manually, and each test starts with a clean slate:
396
+
When using `@StormTest`, a fresh `SqlCapture` instance is automatically injected into each test method that declares it as a parameter. This means you do not need to create one manually, and each test starts with a clean slate:
1.**Keep SQL scripts small and focused.** Each test class should set up only the tables and data it needs. This keeps tests fast and independent.
421
-
2.**Use `StatementCapture` to verify query counts.** Asserting the number of statements an operation produces is an effective way to catch unintended query changes during refactoring.
421
+
2.**Use `SqlCapture` to verify query counts.** Asserting the number of statements an operation produces is an effective way to catch unintended query changes during refactoring.
422
422
3.**Clear between captures** when a single test method needs to measure multiple operations independently.
423
423
4.**Prefer `@StormTest` over manual setup.** It eliminates boilerplate and ensures consistent database lifecycle management across test classes.
424
-
5.**`StatementCapture` is thread-local.** Captures are bound to the calling thread, so multi-threaded tests will only record statements from the thread that called `run`/`execute`.
424
+
5.**`SqlCapture` is thread-local.** Captures are bound to the calling thread, so multi-threaded tests will only record statements from the thread that called `run`/`execute`.
0 commit comments