原文地址html
在测试关系型数据库一篇里咱们使用的是H2数据库,这是为了让你免去你去安装/配置一个数据库的工做,可以尽快的了解到集成测试的过程。mysql
在文章里也说了:git
在真实的开发环境中,集成测试用数据库应该和最终的生产数据库保持一致
那么很容易就能想到两种解决方案:github
那么作到可否这样呢?spring
So, Docker comes to the rescue。sql
咱们仍是会以测试关系型数据库里的FooRepositoryImpl
来作集成测试(代码在这里)。下面来说解具体步骤:docker
请查阅官方文档。而且掌握Docker的基本概念。数据库
farbic8 docker-maven-plugin顾名思义就是一个可以使用docker的maven plugin。它主要功能有二:apache
咱们这里使用启动Docker container的功能。segmentfault
大体配置以下
<plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <version>0.28.0</version> <configuration> <images> <image> <!-- 使用mysql:8 docker image --> <name>mysql:8</name> <!-- 定义docker run mysql:8 时的参数 --> <run> <ports> <!-- host port到container port的映射 这里随机选择一个host port,并将值存到property docker-mysql.port里 --> <port>docker-mysql.port:3306</port> </ports> <!-- 启动时给的环境变量,参阅文档:https://hub.docker.com/_/mysql --> <env> <MYSQL_ROOT_PASSWORD>123456</MYSQL_ROOT_PASSWORD> <MYSQL_DATABASE>test</MYSQL_DATABASE> <MYSQL_USER>foo</MYSQL_USER> <MYSQL_PASSWORD>bar</MYSQL_PASSWORD> </env> <!-- 设置断定container启动成功的的条件及timeout --> <wait> <!-- 若是container打出了这行日志,则说明容器启动成功 --> <log>MySQL init process done. Ready for start up.</log> <time>120000</time> </wait> </run> </image> </images> </configuration> <executions> <execution> <!-- 在集成测试开始前启动容器 --> <id>start</id> <phase>pre-integration-test</phase> <goals> <goal>start</goal> </goals> </execution> <execution> <!-- 在集成测试结束后中止并删除容器 --> <id>stop</id> <phase>post-integration-test</phase> <goals> <goal>stop</goal> </goals> </execution> </executions> </plugin>
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-failsafe-plugin</artifactId> <executions> <execution> <id>integration-test</id> <goals> <goal>integration-test</goal> </goals> </execution> <execution> <id>verify</id> <goals> <goal>verify</goal> </goals> </execution> </executions> <configuration> <!-- 咱们被测的是一个Spring Boot项目,所以能够经过System Properties把MySQL container的相关信息传递给程序 详见文档:https://docs.spring.io/spring-boot/docs/1.5.4.RELEASE/reference/html/boot-features-external-config.html --> <systemPropertyVariables> <spring.datasource.url>jdbc:mysql://localhost:${docker-mysql.port}/test</spring.datasource.url> <spring.datasource.username>foo</spring.datasource.username> <spring.datasource.password>bar</spring.datasource.password> </systemPropertyVariables> </configuration> </plugin>
三种常见用法:
mvn clean integration-test
,会启动docker container、运行集成测试。这个颇有用,若是集成测试失败,那么你还能够链接到MySQL数据库查看状况。mvn clean verify
,会执行mvn integration-test
、删除docker container。mvn clean install
,会执mvn verify
,并将包安装到本地maven 仓库。下面是mvn clean verify
的日志:
... [INFO] --- docker-maven-plugin:0.28.0:start (start) @ spring-test-examples-rdbs-docker --- [INFO] DOCKER> [mysql:8]: Start container f683aadfe8ba [INFO] DOCKER> Pattern 'MySQL init process done. Ready for start up.' matched for container f683aadfe8ba [INFO] DOCKER> [mysql:8]: Waited on log out 'MySQL init process done. Ready for start up.' 13717 ms [INFO] [INFO] --- maven-failsafe-plugin:2.22.1:integration-test (integration-test) @ spring-test-examples-rdbs-docker --- [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- ... [INFO] [INFO] Results: [INFO] [INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] [INFO] --- docker-maven-plugin:0.28.0:stop (stop) @ spring-test-examples-rdbs-docker --- [INFO] DOCKER> [mysql:8]: Stop and removed container f683aadfe8ba after 0 ms [INFO] [INFO] --- maven-failsafe-plugin:2.22.1:verify (verify) @ spring-test-examples-rdbs-docker --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ ...
能够看到fabric8 dmp在集成测试先后start和stop容器的相关日志,且测试成功。
如何找到MySQL的端口开在哪个呢?运行docker ps
查看端口(注意下面的0.0.0.0:32798->3306/tcp
):
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a1f4b51d7c75 mysql:8 ... ... Up 19... 33060/tcp, 0.0.0.0:32798->3306/tcp mysql-1