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
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
+
31
36
### Basic Usage
32
37
33
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.
@@ -52,26 +57,26 @@ public class EmptyDatabaseIntegrationTest {
52
57
53
58
#### Replacing an existing data source with an empty database
54
59
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.
56
61
57
62
```java
58
63
@RunWith(SpringRunner.class)
59
64
@AutoConfigureEmbeddedDatabase
60
-
@ContextConfiguration("/path/to/app-config.xml")
65
+
@ContextConfiguration("path/to/app-config.xml")
61
66
publicclassEmptyDatabaseIntegrationTest {
62
67
// class body...
63
68
}
64
69
```
65
70
66
71
#### Using `@FlywayTest` annotation on a test class
67
72
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.
69
74
70
75
```java
71
76
@RunWith(SpringRunner.class)
72
77
@FlywayTest
73
78
@AutoConfigureEmbeddedDatabase
74
-
@ContextConfiguration("/path/to/app-config.xml")
79
+
@ContextConfiguration("path/to/app-config.xml")
75
80
publicclassFlywayMigrationIntegrationTest {
76
81
// class body...
77
82
}
@@ -87,7 +92,7 @@ See [Usage of Annotation FlywayTest](https://github.com/flyway/flyway-test-exten
@@ -100,7 +105,7 @@ It is also possible to use `@FlywayTest` annotation on a test method. In such ca
100
105
```java
101
106
@RunWith(SpringRunner.class)
102
107
@AutoConfigureEmbeddedDatabase
103
-
@ContextConfiguration("/path/to/app-config.xml")
108
+
@ContextConfiguration("path/to/app-config.xml")
104
109
publicclassFlywayMigrationIntegrationTest {
105
110
106
111
@Test
@@ -111,6 +116,113 @@ public class FlywayMigrationIntegrationTest {
111
116
}
112
117
```
113
118
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
+
publicclassSpringDataJpaAnnotationTest {
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.
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.
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.
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 {
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
+
149
271
## Building from Source
150
272
The project uses a [Gradle](http://gradle.org)-based build system. In the instructions
151
273
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.
Copy file name to clipboardExpand all lines: embedded-database-spring-test/src/main/java/io/zonky/test/db/postgres/embedded/DefaultPostgresBinaryResolver.java
+16Lines changed: 16 additions & 0 deletions
Original file line number
Diff line number
Diff 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
Copy file name to clipboardExpand all lines: embedded-database-spring-test/src/test/java/io/zonky/test/db/AsyncFlywayInitializationIntegrationTest.java
+16Lines changed: 16 additions & 0 deletions
Original file line number
Diff line number
Diff 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
0 commit comments