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
* Support for running in Docker, including Alpine Linux
17
+
* Support for running inside Docker, including Alpine Linux
18
18
19
19
## Quick Start
20
20
@@ -31,15 +31,15 @@ Add the following Maven dependency:
31
31
</dependency>
32
32
```
33
33
34
-
The default version of the embedded database is `PostgreSQL 10.4`, but you can change it using the instructions described in [Changing the version of postgres binaries](#changing-the-version-of-postgres-binaries).
34
+
The default version of the embedded database is `PostgreSQL 10.5`, but you can change it by following the instructions described in [Changing the version of postgres binaries](#changing-the-version-of-postgres-binaries).
35
35
36
36
### Basic Usage
37
37
38
38
The configuration of the embedded database is driven by `@AutoConfigureEmbeddedDatabase` annotation. Just place the annotation on a test class and that's it! The existing data source will be replaced by the testing one, or a new data source will be created.
39
39
40
-
###Examples
40
+
## Examples
41
41
42
-
####Creating a new empty database with a specified bean name
42
+
### Creating a new empty database with a specified bean name
43
43
44
44
A new data source with a specified name will be created and injected into all related components. You can also inject it into test class as shown below.
45
45
@@ -55,34 +55,34 @@ public class EmptyDatabaseIntegrationTest {
55
55
}
56
56
```
57
57
58
-
####Replacing an existing data source with an empty database
58
+
### Replacing an existing data source with an empty database
59
59
60
-
In case the test class uses a context configuration that already contains a data source bean, it will be automatically replaced by the testing data source. Please note that if the context contains multiple data sources the bean name must be specified by `@AutoConfigureEmbeddedDatabase(beanName = "dataSource")` to identify the data source that will be replaced. The newly created data source bean will be injected into all related components and you can also inject it into test class.
60
+
In case the test class uses a spring context that already contains a data source bean, the data source bean will be automatically replaced by a testing data source. Please note that if the context contains multiple data sources the bean name must be specified by `@AutoConfigureEmbeddedDatabase(beanName = "dataSource")` to identify the data source that will be replaced. The newly created data source bean will be injected into all related components and you can also inject it into test class.
####Using `@FlywayTest` annotation on a test class
71
+
### Using `@FlywayTest` annotation on a test class
72
72
73
73
The library supports the use of `@FlywayTest` annotation. When you use it, the embedded database will be automatically initialized and cleaned by Flyway database migration tool. If you don't specify any custom migration locations the default path `db/migration` will be applied.
####Using `@FlywayTest` annotation with additional options
85
+
### Using `@FlywayTest` annotation with additional options
86
86
87
87
In case you want to apply migrations from some additional locations, you can use `@FlywayTest(locationsForMigrate = "path/to/migrations")` configuration. In this case, the sql scripts from the default location and also sql scripts from the additional locations will be applied. If you need to prevent the loading of the scripts from the default location you can use `@FlywayTest(overrideLocations = true, ...)` annotation configuration.
88
88
@@ -92,20 +92,20 @@ See [Usage of Annotation FlywayTest](https://github.com/flyway/flyway-test-exten
####Using `@FlywayTest` annotation on a test method
101
+
### Using `@FlywayTest` annotation on a test method
102
102
103
103
It is also possible to use `@FlywayTest` annotation on a test method. In such case, the isolated embedded database will be created and managed for the duration of the test method. If you don't specify any custom migration locations the default path `db/migration` will be applied.
@@ -116,12 +116,12 @@ public class FlywayMigrationIntegrationTest {
116
116
}
117
117
```
118
118
119
-
####Using `@AutoConfigureEmbeddedDatabase` and`@DataJpaTest` annotations together
119
+
### Using `@DataJpaTest` or`@JdbcTest` annotation
120
120
121
121
Spring Boot provides several annotations to simplify writing integration tests.
122
122
One of them is the `@DataJpaTest` annotation, which can be used when a test focuses only on JPA components.
123
123
By default, tests annotated with `@DataJpaTest` will use an embedded in-memory database. **This in-memory database can be H2, Derby or HSQL, but not the PostgreSQL database**.
124
-
To change that, you must first disable the default in-memory database by `@AutoConfigureTestDatabase(replace = NONE)` and enable the PostgreSQL database by `@AutoConfigureEmbeddedDatabase` instead.
124
+
To change that, you must first disable the default in-memory database by `@AutoConfigureTestDatabase(replace = NONE)` and enable PostgreSQL database by `@AutoConfigureEmbeddedDatabase` instead.
125
125
126
126
```java
127
127
@RunWith(SpringRunner.class)
@@ -134,9 +134,42 @@ public class SpringDataJpaAnnotationTest {
134
134
```
135
135
You can also consider creating a custom [composed annotation](https://github.com/spring-projects/spring-framework/wiki/Spring-Annotation-Programming-Model#composed-annotations).
136
136
137
-
## Advanced
137
+
<details>
138
+
<summary>Example of composed annotation</summary>
139
+
140
+
```java
141
+
@Documented
142
+
@Inherited
143
+
@Target(ElementType.TYPE)
144
+
@Retention(RetentionPolicy.RUNTIME)
145
+
@AutoConfigureTestDatabase(replace=Replace.NONE)
146
+
@AutoConfigureEmbeddedDatabase
147
+
@DataJpaTest
148
+
public@interfacePostgresDataJpaTest {
149
+
150
+
@AliasFor(annotation=DataJpaTest.class)
151
+
booleanshowSql() default true;
152
+
153
+
@AliasFor(annotation=DataJpaTest.class)
154
+
booleanuseDefaultFilters() default true;
155
+
156
+
@AliasFor(annotation=DataJpaTest.class)
157
+
Filter[] includeFilters() default {};
158
+
159
+
@AliasFor(annotation=DataJpaTest.class)
160
+
Filter[] excludeFilters() default {};
161
+
162
+
@AliasFor(annotation=DataJpaTest.class)
163
+
Class<?>[] excludeAutoConfiguration() default {};
164
+
165
+
}
166
+
```
167
+
168
+
</details>
138
169
139
-
#### Changing the version of postgres binaries
170
+
## Advanced Topics
171
+
172
+
### Changing the version of postgres binaries
140
173
141
174
Changing the version is performed by importing `embedded-postgres-binaries-bom` in the required version into your dependency management section.
142
175
@@ -146,7 +179,7 @@ Changing the version is performed by importing `embedded-postgres-binaries-bom`
@@ -158,10 +191,10 @@ A list of all available versions of postgres binaries is here: https://mvnreposi
158
191
159
192
Note that the release cycle of the postgres binaries is independent of the release cycle of this library, so you can upgrade to the new version of postgres binaries immediately after it is released.
160
193
161
-
####Enabling support for additional architectures
194
+
### Enabling support for additional architectures
162
195
163
196
By default, only the support for `amd64` architecture is enabled.
164
-
Support for other architectures can be enabled by adding the corresponding Maven dependency as shown in the example below.
197
+
Support for other architectures can be enabled by adding the corresponding Maven dependencies as shown in the example below.
165
198
166
199
```xml
167
200
<dependency>
@@ -176,9 +209,9 @@ Support for other architectures can be enabled by adding the corresponding Maven
176
209
177
210
Note that not all architectures are supported by all platforms, look here for an exhaustive list of all available artifacts: https://mvnrepository.com/artifact/io.zonky.test.postgres
178
211
179
-
Since `PostgreSQL 10.0` there are available special `alpine-lite`artifacts, which contain postgres binaries for Alpine Linux with disabled [ICU support](https://blog.2ndquadrant.com/icu-support-postgresql-10/) for further size reduction.
212
+
Since `PostgreSQL 10.0`, there are additional artifacts with `alpine-lite`suffix. These artifacts contain postgres binaries for Alpine Linux but with disabled [ICU support](https://blog.2ndquadrant.com/icu-support-postgresql-10/) for further size reduction.
180
213
181
-
####Disabling auto-configuration
214
+
### Disabling auto-configuration
182
215
183
216
By default, the library automatically registers all necessary context customizers and test execution listeners.
184
217
If this behavior is inappropriate for some reason, you can deactivate it by exclusion of the `embedded-database-spring-test-autoconfigure` dependency.
@@ -198,7 +231,7 @@ If this behavior is inappropriate for some reason, you can deactivate it by excl
198
231
</dependency>
199
232
```
200
233
201
-
####Customizing database configuration
234
+
### Customizing database configuration
202
235
203
236
If necessary, you can customize the configuration of the embedded database with bean implementing `Consumer<EmbeddedPostgres.Builder>` interface.
204
237
The obtained builder provides methods to change the configuration before the database is started.
@@ -223,7 +256,7 @@ public class EmbeddedPostgresIntegrationTest {
223
256
}
224
257
```
225
258
226
-
####Background bootstrapping mode
259
+
### Background bootstrapping mode
227
260
228
261
Using this feature causes that the initialization of the data source and the execution of Flyway database migrations are performed in background bootstrap mode.
229
262
In such case, a `DataSource` proxy is immediately returned for injection purposes instead of waiting for the Flyway's bootstrapping to complete.
@@ -259,11 +292,61 @@ public class FlywayMigrationIntegrationTest {
259
292
260
293
## Troubleshooting
261
294
262
-
#### Running tests in Docker does not work
295
+
### Connecting to embedded database
296
+
297
+
When you use a breakpoint to pause the tests, you can connect to a temporary embedded database. Connection details can be found in the log as shown in the example below:
298
+
299
+
```log
300
+
i.z.t.d.l.EmbeddedDatabaseReporter - JDBC URL to connect to the embedded database: jdbc:postgresql://localhost:55112/fynwkrpzfcyj?user=postgres, scope: TestClass#testMethod
301
+
```
302
+
303
+
If you are using `@FlywayTest` annotation, there may be several similar records in the log but with different scopes. That's because in such case multiple isolated databases can be created.
304
+
305
+
### Process [/tmp/embedded-pg/PG-XYZ/bin/initdb, ...] failed
306
+
307
+
Try to remove `/tmp/embedded-pg/PG-XYZ` directory containing temporary binaries of the embedded postgres database. That should solve the problem.
308
+
309
+
### Running tests on Windows does not work
310
+
311
+
You probably need to install the [Microsoft Visual C++ 2013 Redistributable Package](https://support.microsoft.com/en-us/help/3179560/update-for-visual-c-2013-and-visual-c-redistributable-package). The version 2013 is important, installation of other versions will not help. More detailed is the problem discussed [here](https://github.com/opentable/otj-pg-embedded/issues/65).
312
+
313
+
### Running tests inside Docker does not work
314
+
315
+
Running build inside Docker is fully supported, including Alpine Linux. But you must keep in mind that the **PostgreSQL database requires running under a non-root user**. Otherwise, the database does not start and fails with an error.
316
+
317
+
So be sure to use a docker image that uses the non-root user, or you can use any of the following Dockerfiles to build your own image.
318
+
319
+
<details>
320
+
<summary>Standard Dockerfile</summary>
321
+
322
+
```dockerfile
323
+
FROM openjdk:8-jdk
324
+
325
+
RUN groupadd --system --gid 1000 test
326
+
RUN useradd --system --gid test --uid 1000 --shell /bin/bash --create-home test
327
+
328
+
USER test
329
+
WORKDIR /home/test
330
+
```
331
+
332
+
</details>
333
+
334
+
<details>
335
+
<summary>Alpine Dockerfile</summary>
336
+
337
+
```dockerfile
338
+
FROM openjdk:8-jdk-alpine
339
+
340
+
RUN addgroup -S -g 1000 test
341
+
RUN adduser -D -S -G test -u 1000 -s /bin/ash test
342
+
343
+
USER test
344
+
WORKDIR /home/test
345
+
```
263
346
264
-
Running in Docker is fully supported, including Alpine Linux. But you must keep in mind that the **PostgreSQL database requires running under a non-root user**. Otherwise, the database does not start and fails with an error.
347
+
</details>
265
348
266
-
####Frequent and repeated initialization of the database
349
+
### Frequent and repeated initialization of the database
267
350
268
351
Make sure that you do not use `org.flywaydb.test.junit.FlywayTestExecutionListener`. Because this library has its own test execution listener that can optimize database initialization.
269
352
But this optimization has no effect if `FlywayTestExecutionListener` is applied.
@@ -288,8 +371,9 @@ extracted from the JDK download.
0 commit comments