简体   繁体   English

如何使用在docker中运行的mysql运行maven测试(使用maven-surefire-plugin)

[英]How to run maven test with mysql running in docker (using maven-surefire-plugin)

I found several post that explains how to run integration tests wising maven and docker. 我发现了几篇文章,解释了如何运行wising Maven和docker的集成测试。 They basically explains hoy to use/setup fabric8 maven-docker-plugin and maven failsafe plugin. 他们基本上解释了hoy使用/设置fabric8 maven-docker-plugin和maven故障安全插件。

I'm wondering if is possible to use fabric8, for example, with surefire. 我想知道是否可以在例如surefire中使用fabric8。 My specific need is: I need to run my tests (JUnit tests) but first I need/want to start a docker image with MySQL running on it. 我的特定需求是:我需要运行测试(JUnit测试),但是首先,我需要/想要在其上运行MySQL的情况下启动docker镜像。

I'm not tied to fabric8. 我不依赖于fabric8。 If there is another way to start the docker image before start my tests and stop/kill it once tests run finish, that will help me a lot. 如果还有其他方法可以在开始测试之前启动docker映像,并在测试运行完成后停止/杀死它,那将对我有很大帮助。

Thanks in advance. 提前致谢。 Maxi 马克西

Yes you can definitely do that. 是的,您绝对可以做到。 The idea is to use a maven docker plugin such as fabric8 docker plugin and start a container before the test phases and stop it after the test phase. 这个想法是使用maven docker插件(例如fabric8 docker插件)并在测试阶段之前启动容器,并在测试阶段之后停止容器。 But note that technically tests that connect to databases are not unit tests, they should be integration tests. 但是请注意,从技术上讲,连接到数据库的测试不是单元测试,而应该是集成测试。

There are many tutorials online to do that such as INTEGRATION TESTING WITH DOCKER AND MAVEN 在线上有许多教程可以做到这一点,例如使用DOCKER和MAVEN进行集成测试

You can adapt this for unit tests by just changing the phases when the docker plugin runs. 您可以通过更改docker插件运行时的阶段来使其适应单元测试。

You can change <phase>pre-integration-test</phase> to a phase that starts before the maven test phase such as <phase>generate-test-resources</phase> and stop the container once the test ends for example: <phase>prepare-package</phase> . 您可以将<phase>pre-integration-test</phase>更改为在maven test阶段之前开始的阶段,例如<phase>generate-test-resources</phase>并在测试结束后停止容器,例如: <phase>prepare-package</phase>

Note that there are no nice phase names as there are for integration test, as it is not ideal to start external services when running unit tests. 请注意,没有像集成测试那样好的名称,因为在运行单元测试时启动外部服务并不理想。 But anyway it works. 但是无论如何,它是可行的。

For a complete reference on maven phases check Introduction to the Build Lifecycle 有关Maven阶段的完整参考,请查看Build Lifecycle简介。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 运行 maven-surefire-plugin:test 时出现 lombok 编译错误 - lombok compilation errors when running maven-surefire-plugin:test JUnit 和 Spock 测试与 maven-surefire-plugin - JUnit and Spock test with maven-surefire-plugin Maven maven-surefire-plugin 并非所有测试都在运行 - Maven maven-surefire-plugin not all tests are running 带有Maven-surefire-plugin的UnsatisfiedLinkError - UnsatisfiedLinkError with maven-surefire-plugin 在 Jenkins 中运行 mvn test 时 maven-surefire-plugin 的 Java 版本问题 - Java version issue with maven-surefire-plugin while running mvn test in Jenkins maven-surefire-plugin包含单一测试不起作用 - maven-surefire-plugin inclusion of Single test is not working 如何在控制台上显示maven-surefire-plugin单元测试覆盖率报告 - How to show maven-surefire-plugin unit test coverage report on console 从maven运行TestNG套件获取错误:maven-surefire-plugin:test failed:testSuiteXmlFiles0具有null值 - Run TestNG suite from maven getting error:maven-surefire-plugin:test failed: testSuiteXmlFiles0 has null value 如何使 maven-surefire-plugin 与 Eclipse 中的 TestNG 一起工作 - How to make maven-surefire-plugin work with TestNG in Eclipse TestNg的Maven-surefire-plugin:如何指定存储测试套件xml文件的目录? - Maven-surefire-plugin with TestNg : how to specify the directory where test suites xml files are stored?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM