Skip to content

Commit 2473303

Browse files
committed
update documentation
1 parent 1778211 commit 2473303

File tree

9 files changed

+257
-25
lines changed

9 files changed

+257
-25
lines changed

README.md

Lines changed: 142 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,35 @@ The primary goal of this project is to make it easier to write Spring-powered in
44

55
## Features
66

7-
* Integration with Spring TestContext framework
8-
* Context caching is fully supported
9-
* Both `Spring` and `Spring Boot` frameworks are supported
7+
* Supports both `Spring` and `Spring Boot` frameworks
108
* Supported versions are Spring 4.3.0+ and Spring Boot 1.4.0+
11-
* Lightweight bundle of PostgreSQL database with reduced size
12-
* Integration with Flyway database migration tool
13-
* Optimized migration and cleaning of embedded database
9+
* Automatic integration with Spring TestContext framework
10+
* Context caching is fully supported
11+
* Seamless integration with Flyway database migration tool
12+
* Just place `@FlywayTest` annotation on test class or method
13+
* Optimized initialization and cleaning of embedded databases
1414
* Database templates are used to reduce the loading time
15+
* Uses lightweight bundles of PostgreSQL binaries with reduced size
16+
* https://github.com/zonkyio/embedded-postgres-binaries
17+
* Support for running in Docker, including Alpine Linux
1518

1619
## Quick Start
1720

1821
### Maven Configuration
1922

20-
Add the Maven dependency:
23+
Add the following Maven dependency:
2124

2225
```xml
2326
<dependency>
24-
<groupId>io.zonky.test</groupId>
25-
<artifactId>embedded-database-spring-test</artifactId>
26-
<version>1.2.0</version>
27-
<scope>test</scope>
27+
<groupId>io.zonky.test</groupId>
28+
<artifactId>embedded-database-spring-test</artifactId>
29+
<version>1.2.0</version>
30+
<scope>test</scope>
2831
</dependency>
2932
```
3033

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).
35+
3136
### Basic Usage
3237

3338
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.
@@ -52,26 +57,26 @@ public class EmptyDatabaseIntegrationTest {
5257

5358
#### Replacing an existing data source with an empty database
5459

55-
In case the test class uses a context configuration that already contains a data source, 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 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.
5661

5762
```java
5863
@RunWith(SpringRunner.class)
5964
@AutoConfigureEmbeddedDatabase
60-
@ContextConfiguration("/path/to/app-config.xml")
65+
@ContextConfiguration("path/to/app-config.xml")
6166
public class EmptyDatabaseIntegrationTest {
6267
// class body...
6368
}
6469
```
6570

6671
#### Using `@FlywayTest` annotation on a test class
6772

68-
The library supports the use of `@FlywayTest` annotation. When you use it, the embedded database will automatically be initialized by Flyway database migration tool. If you don't specify any custom migration locations the default path `db/migration` will be applied.
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.
6974

7075
```java
7176
@RunWith(SpringRunner.class)
7277
@FlywayTest
7378
@AutoConfigureEmbeddedDatabase
74-
@ContextConfiguration("/path/to/app-config.xml")
79+
@ContextConfiguration("path/to/app-config.xml")
7580
public class FlywayMigrationIntegrationTest {
7681
// class body...
7782
}
@@ -87,7 +92,7 @@ See [Usage of Annotation FlywayTest](https://github.com/flyway/flyway-test-exten
8792
@RunWith(SpringRunner.class)
8893
@FlywayTest(locationsForMigrate = "test/db/migration")
8994
@AutoConfigureEmbeddedDatabase
90-
@ContextConfiguration("/path/to/app-config.xml")
95+
@ContextConfiguration("path/to/app-config.xml")
9196
public class FlywayMigrationIntegrationTest {
9297
// class body...
9398
}
@@ -100,7 +105,7 @@ It is also possible to use `@FlywayTest` annotation on a test method. In such ca
100105
```java
101106
@RunWith(SpringRunner.class)
102107
@AutoConfigureEmbeddedDatabase
103-
@ContextConfiguration("/path/to/app-config.xml")
108+
@ContextConfiguration("path/to/app-config.xml")
104109
public class FlywayMigrationIntegrationTest {
105110

106111
@Test
@@ -111,6 +116,113 @@ public class FlywayMigrationIntegrationTest {
111116
}
112117
```
113118

119+
#### Using `@AutoConfigureEmbeddedDatabase` and `@DataJpaTest` annotations together
120+
121+
Spring Boot provides several annotations to simplify writing integration tests.
122+
One of them is the `@DataJpaTest` annotation, which can be used when a test focuses only on JPA components.
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.
125+
126+
```java
127+
@RunWith(SpringRunner.class)
128+
@AutoConfigureTestDatabase(replace = NONE)
129+
@AutoConfigureEmbeddedDatabase
130+
@DataJpaTest
131+
public class SpringDataJpaAnnotationTest {
132+
// class body...
133+
}
134+
```
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+
137+
## Advanced
138+
139+
#### Changing the version of postgres binaries
140+
141+
Changing the version is performed by importing `embedded-postgres-binaries-bom` in the required version into your dependency management section.
142+
143+
```xml
144+
<dependencyManagement>
145+
<dependencies>
146+
<dependency>
147+
<groupId>io.zonky.test.postgres</groupId>
148+
<artifactId>embedded-postgres-binaries-bom</artifactId>
149+
<version>10.4.0</version>
150+
<type>pom</type>
151+
<scope>import</scope>
152+
</dependency>
153+
</dependencies>
154+
</dependencyManagement>
155+
```
156+
157+
A list of all available versions of postgres binaries is here: https://mvnrepository.com/artifact/io.zonky.test.postgres/embedded-postgres-binaries-bom
158+
159+
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+
161+
#### Enabling support for additional architectures
162+
163+
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.
165+
166+
```xml
167+
<dependency>
168+
<groupId>io.zonky.test.postgres</groupId>
169+
<artifactId>embedded-postgres-binaries-linux-i386</artifactId>
170+
<scope>test</scope>
171+
</dependency>
172+
```
173+
174+
**Supported platforms:** `Darwin`, `Windows`, `Linux`, `Alpine Linux`
175+
**Supported architectures:** `amd64`, `i386`, `arm32v6`, `arm32v7`, `arm64v8`, `ppc64le`
176+
177+
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+
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.
180+
181+
#### Disabling auto-configuration
182+
183+
By default, the library automatically registers all necessary context customizers and test execution listeners.
184+
If this behavior is inappropriate for some reason, you can deactivate it by exclusion of the `embedded-database-spring-test-autoconfigure` dependency.
185+
186+
```xml
187+
<dependency>
188+
<groupId>io.zonky.test</groupId>
189+
<artifactId>embedded-database-spring-test</artifactId>
190+
<version>1.3.0</version>
191+
<scope>test</scope>
192+
<exclusions>
193+
<exclusion>
194+
<groupId>io.zonky.test</groupId>
195+
<artifactId>embedded-database-spring-test-autoconfigure</artifactId>
196+
</exclusion>
197+
</exclusions>
198+
</dependency>
199+
```
200+
201+
#### Customizing database configuration
202+
203+
If necessary, you can customize the configuration of the embedded database with bean implementing `Consumer<EmbeddedPostgres.Builder>` interface.
204+
The obtained builder provides methods to change the configuration before the database is started.
205+
206+
```java
207+
@Configuration
208+
public class EmbeddedPostgresConfiguration {
209+
210+
@Bean
211+
public Consumer<EmbeddedPostgres.Builder> embeddedPostgresCustomizer() {
212+
return builder -> builder.setPGStartupWait(Duration.ofSeconds(60L));
213+
}
214+
}
215+
```
216+
217+
```java
218+
@RunWith(SpringRunner.class)
219+
@AutoConfigureEmbeddedDatabase
220+
@ContextConfiguration(classes = EmbeddedPostgresConfiguration.class)
221+
public class EmbeddedPostgresIntegrationTest {
222+
// class body...
223+
}
224+
```
225+
114226
#### Background bootstrapping mode
115227

116228
Using this feature causes that the initialization of the data source and the execution of Flyway database migrations are performed in background bootstrap mode.
@@ -138,14 +250,24 @@ public class BootstrappingConfiguration {
138250

139251
```java
140252
@RunWith(SpringRunner.class)
141-
@FlywayTest
142253
@AutoConfigureEmbeddedDatabase
143254
@ContextConfiguration(classes = BootstrappingConfiguration.class)
144255
public class FlywayMigrationIntegrationTest {
145256
// class body...
146257
}
147258
```
148259

260+
## Troubleshooting
261+
262+
#### Running tests in Docker does not work
263+
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.
265+
266+
#### Frequent and repeated initialization of the database
267+
268+
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+
But this optimization has no effect if `FlywayTestExecutionListener` is applied.
270+
149271
## Building from Source
150272
The project uses a [Gradle](http://gradle.org)-based build system. In the instructions
151273
below, [`./gradlew`](http://vimeo.com/34436402) is invoked from the root of the source tree and serves as
@@ -164,15 +286,11 @@ extracted from the JDK download.
164286
### Compile and test
165287
`./gradlew build`
166288

167-
## Supported databases
168-
169-
* [PostgreSQL](https://www.postgresql.org/) (9.6.3)
170-
171289
## Project dependencies
172290

173291
* [Spring Framework](http://www.springsource.org/) (4.3.10) - `spring-test`, `spring-context` modules
174-
* [OpenTable Embedded PostgreSQL Component](https://github.com/opentable/otj-pg-embedded) (0.9.0)
175-
* [Flyway](https://github.com/flyway/) (4.2.0)
292+
* [OpenTable Embedded PostgreSQL Component](https://github.com/opentable/otj-pg-embedded) (0.12.0)
293+
* [Flyway](https://github.com/flyway/) (5.0.7)
176294
* [Guava](https://github.com/google/guava) (23.0)
177295

178296
## License

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,9 @@ project(':embedded-database-spring-test') {
287287
task updateReadmeVersion() {
288288
doLast {
289289
def version = project.version.toString()
290-
ant.replaceregexp(file: 'README.md', match: '<version>\\d+\\.\\d+\\.\\d+</version>', replace: "<version>$version</version>", flags: 'g', byline: true)
290+
ant.replaceregexp(file: 'README.md',
291+
match: '(?<=<artifactId>embedded-database-spring-test</artifactId>\\n\\s{4,12})<version>\\d+\\.\\d+\\.\\d+</version>',
292+
replace: "<version>$version</version>", flags: 'g')
291293
}
292294
}
293295

embedded-database-spring-test/src/main/java/io/zonky/test/db/postgres/embedded/DefaultPostgresBinaryResolver.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.zonky.test.db.postgres.embedded;
218

319
import com.opentable.db.postgres.embedded.PgBinaryResolver;

embedded-database-spring-test/src/main/java/io/zonky/test/db/util/LinuxUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.zonky.test.db.util;
218

319
import org.apache.commons.lang3.StringUtils;

embedded-database-spring-test/src/test/java/io/zonky/test/category/FlywayIntegrationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.zonky.test.category;
218

319
public interface FlywayIntegrationTests {

embedded-database-spring-test/src/test/java/io/zonky/test/category/MultiFlywayIntegrationTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.zonky.test.category;
218

319
public interface MultiFlywayIntegrationTests {

embedded-database-spring-test/src/test/java/io/zonky/test/db/AsyncFlywayInitializationIntegrationTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.zonky.test.db;
218

319
import com.google.common.base.Stopwatch;

embedded-database-spring-test/src/test/java/io/zonky/test/db/DatabaseCustomizerIntegrationTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package io.zonky.test.db;
218

319
import com.opentable.db.postgres.embedded.EmbeddedPostgres;

0 commit comments

Comments
 (0)