简体   繁体   中英

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. They basically explains hoy to use/setup fabric8 maven-docker-plugin and maven failsafe plugin.

I'm wondering if is possible to use fabric8, for example, with surefire. 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.

I'm not tied to 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.

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

You can adapt this for unit tests by just changing the phases when the docker plugin runs.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM