File tree Expand file tree Collapse file tree 2 files changed +5
-31
lines changed
docs/test_framework_integration
modules/spock/src/test/groovy/org/testcontainers/spock Expand file tree Collapse file tree 2 files changed +5
-31
lines changed Original file line number Diff line number Diff line change 99Specifying the ` @Testcontainers ` annotation will instruct Spock to start and stop all testcontainers accordingly. This annotation
1010can be mixed with Spock's ` @Shared ` annotation to indicate, that containers shouldn't be restarted between tests.
1111
12- ``` groovy
13- @Testcontainers
14- class DatabaseTest extends Specification {
15-
16- @Shared
17- PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer()
18- .withDatabaseName("foo")
19- .withUsername("foo")
20- .withPassword("secret")
21-
22- def "database is accessible"() {
23-
24- given: "a jdbc connection"
25- HikariConfig hikariConfig = new HikariConfig()
26- hikariConfig.setJdbcUrl(postgreSQLContainer.jdbcUrl)
27- hikariConfig.setUsername("foo")
28- hikariConfig.setPassword("secret")
29- HikariDataSource ds = new HikariDataSource(hikariConfig)
30-
31- when: "querying the database"
32- Statement statement = ds.getConnection().createStatement()
33- statement.execute("SELECT 1")
34- ResultSet resultSet = statement.getResultSet()
35- resultSet.next()
36-
37- then: "result is returned"
38- int resultSetInt = resultSet.getInt(1)
39- resultSetInt == 1
40- }
41- }
42- ```
12+ <!-- codeinclude-->
13+ [ PostgresContainerIT] ( ../../modules/spock/src/test/groovy/org/testcontainers/spock/PostgresContainerIT.groovy ) inside_block: PostgresContainerIT
14+ <!-- /codeinclude-->
4315
4416## Adding Testcontainers Spock support to your project dependencies
4517
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ import spock.lang.Specification
99import java.sql.ResultSet
1010import java.sql.Statement
1111
12+ // PostgresContainerIT {
1213@Testcontainers
1314class PostgresContainerIT extends Specification {
1415
@@ -42,3 +43,4 @@ class PostgresContainerIT extends Specification {
4243 }
4344
4445}
46+ // }
You can’t perform that action at this time.
0 commit comments