Skip to content

Commit de695b7

Browse files
committed
update documentation
1 parent 23d61fa commit de695b7

File tree

1 file changed

+112
-28
lines changed

1 file changed

+112
-28
lines changed

README.md

Lines changed: 112 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The primary goal of this project is to make it easier to write Spring-powered in
1414
* Database templates are used to reduce the loading time
1515
* Uses lightweight bundles of PostgreSQL binaries with reduced size
1616
* https://github.com/zonkyio/embedded-postgres-binaries
17-
* Support for running in Docker, including Alpine Linux
17+
* Support for running inside Docker, including Alpine Linux
1818

1919
## Quick Start
2020

@@ -31,15 +31,15 @@ Add the following Maven dependency:
3131
</dependency>
3232
```
3333

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).
3535

3636
### Basic Usage
3737

3838
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.
3939

40-
### Examples
40+
## Examples
4141

42-
#### Creating a new empty database with a specified bean name
42+
### Creating a new empty database with a specified bean name
4343

4444
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.
4545

@@ -55,34 +55,34 @@ public class EmptyDatabaseIntegrationTest {
5555
}
5656
```
5757

58-
#### Replacing an existing data source with an empty database
58+
### Replacing an existing data source with an empty database
5959

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.
6161

6262
```java
6363
@RunWith(SpringRunner.class)
6464
@AutoConfigureEmbeddedDatabase
65-
@ContextConfiguration("path/to/app-config.xml")
65+
@ContextConfiguration("path/to/application-config.xml")
6666
public class EmptyDatabaseIntegrationTest {
6767
// class body...
6868
}
6969
```
7070

71-
#### Using `@FlywayTest` annotation on a test class
71+
### Using `@FlywayTest` annotation on a test class
7272

7373
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.
7474

7575
```java
7676
@RunWith(SpringRunner.class)
7777
@FlywayTest
7878
@AutoConfigureEmbeddedDatabase
79-
@ContextConfiguration("path/to/app-config.xml")
79+
@ContextConfiguration("path/to/application-config.xml")
8080
public class FlywayMigrationIntegrationTest {
8181
// class body...
8282
}
8383
```
8484

85-
#### Using `@FlywayTest` annotation with additional options
85+
### Using `@FlywayTest` annotation with additional options
8686

8787
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.
8888

@@ -92,20 +92,20 @@ See [Usage of Annotation FlywayTest](https://github.com/flyway/flyway-test-exten
9292
@RunWith(SpringRunner.class)
9393
@FlywayTest(locationsForMigrate = "test/db/migration")
9494
@AutoConfigureEmbeddedDatabase
95-
@ContextConfiguration("path/to/app-config.xml")
95+
@ContextConfiguration("path/to/application-config.xml")
9696
public class FlywayMigrationIntegrationTest {
9797
// class body...
9898
}
9999
```
100100

101-
#### Using `@FlywayTest` annotation on a test method
101+
### Using `@FlywayTest` annotation on a test method
102102

103103
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.
104104

105105
```java
106106
@RunWith(SpringRunner.class)
107107
@AutoConfigureEmbeddedDatabase
108-
@ContextConfiguration("path/to/app-config.xml")
108+
@ContextConfiguration("path/to/application-config.xml")
109109
public class FlywayMigrationIntegrationTest {
110110

111111
@Test
@@ -116,12 +116,12 @@ public class FlywayMigrationIntegrationTest {
116116
}
117117
```
118118

119-
#### Using `@AutoConfigureEmbeddedDatabase` and `@DataJpaTest` annotations together
119+
### Using `@DataJpaTest` or `@JdbcTest` annotation
120120

121121
Spring Boot provides several annotations to simplify writing integration tests.
122122
One of them is the `@DataJpaTest` annotation, which can be used when a test focuses only on JPA components.
123123
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.
125125

126126
```java
127127
@RunWith(SpringRunner.class)
@@ -134,9 +134,42 @@ public class SpringDataJpaAnnotationTest {
134134
```
135135
You can also consider creating a custom [composed annotation](https://github.com/spring-projects/spring-framework/wiki/Spring-Annotation-Programming-Model#composed-annotations).
136136

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 @interface PostgresDataJpaTest {
149+
150+
@AliasFor(annotation = DataJpaTest.class)
151+
boolean showSql() default true;
152+
153+
@AliasFor(annotation = DataJpaTest.class)
154+
boolean useDefaultFilters() 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>
138169

139-
#### Changing the version of postgres binaries
170+
## Advanced Topics
171+
172+
### Changing the version of postgres binaries
140173

141174
Changing the version is performed by importing `embedded-postgres-binaries-bom` in the required version into your dependency management section.
142175

@@ -146,7 +179,7 @@ Changing the version is performed by importing `embedded-postgres-binaries-bom`
146179
<dependency>
147180
<groupId>io.zonky.test.postgres</groupId>
148181
<artifactId>embedded-postgres-binaries-bom</artifactId>
149-
<version>10.4.0</version>
182+
<version>10.5.0</version>
150183
<type>pom</type>
151184
<scope>import</scope>
152185
</dependency>
@@ -158,10 +191,10 @@ A list of all available versions of postgres binaries is here: https://mvnreposi
158191

159192
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.
160193

161-
#### Enabling support for additional architectures
194+
### Enabling support for additional architectures
162195

163196
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.
165198

166199
```xml
167200
<dependency>
@@ -176,9 +209,9 @@ Support for other architectures can be enabled by adding the corresponding Maven
176209

177210
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
178211

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.
180213

181-
#### Disabling auto-configuration
214+
### Disabling auto-configuration
182215

183216
By default, the library automatically registers all necessary context customizers and test execution listeners.
184217
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
198231
</dependency>
199232
```
200233

201-
#### Customizing database configuration
234+
### Customizing database configuration
202235

203236
If necessary, you can customize the configuration of the embedded database with bean implementing `Consumer<EmbeddedPostgres.Builder>` interface.
204237
The obtained builder provides methods to change the configuration before the database is started.
@@ -223,7 +256,7 @@ public class EmbeddedPostgresIntegrationTest {
223256
}
224257
```
225258

226-
#### Background bootstrapping mode
259+
### Background bootstrapping mode
227260

228261
Using this feature causes that the initialization of the data source and the execution of Flyway database migrations are performed in background bootstrap mode.
229262
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 {
259292

260293
## Troubleshooting
261294

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+
```
263346

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>
265348

266-
#### Frequent and repeated initialization of the database
349+
### Frequent and repeated initialization of the database
267350

268351
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.
269352
But this optimization has no effect if `FlywayTestExecutionListener` is applied.
@@ -288,8 +371,9 @@ extracted from the JDK download.
288371

289372
## Project dependencies
290373

291-
* [Spring Framework](http://www.springsource.org/) (4.3.10) - `spring-test`, `spring-context` modules
374+
* [PostgreSQL Binaries](https://github.com/zonkyio/embedded-postgres-binaries) (10.5)
292375
* [OpenTable Embedded PostgreSQL Component](https://github.com/opentable/otj-pg-embedded) (0.12.0)
376+
* [Spring Framework](http://www.springsource.org/) (4.3.18) - `spring-test`, `spring-context` modules
293377
* [Flyway](https://github.com/flyway/) (5.0.7)
294378
* [Guava](https://github.com/google/guava) (23.0)
295379

0 commit comments

Comments
 (0)