I have implemented a Maven plugin which is used to create test database (with random name) before Maven test
phase, and drops that database when the test
phase is completed.
The plugin need to be executed two times, before test
phase (when is used to create database) and after test
phase (when is used to drop that test database).
Which Maven lifecycle phase will be always executed after test phase, whether test
phase is successfully executed or not?
There are no particular phase in the Maven lifecycle that corresponds to pre- and post-test. This is because unit tests are not supposed to require an external environment. It sounds like what you want to do are not unit tests but integration tests instead, because they require an environment to be set up.
From the docs:
And there is a
pre-integration-test
,integration-test
andpost-integration-test
that are used to setup, run and destroy the test environment.As such, it would be easier and a lot cleaner to do this in
integration-test
phase using themaven-failsafe-plugin
.Now, if you really want to run that as unit tests, I would not write the creation / deletion of the database as a Maven plugin. It would be a lot better to let your application create the test database when it is configured in a test environment. (For example, if you're using Spring, it has a lot of facilities for that.)
And, if you really want to run that as unit tests in the
test
phase, and using your plugin, you will have to skip the default execution of themaven-surefire-plugin
and then define an execution of your Maven plugin creating the database, a new execution of themaven-surefire-plugin
and an execution of your Maven plugin dropping the database, bound to thetest
phase.This works because Maven invokes the plugins in the order as they are defined in the POM when they're bound to the same phase.
A configuration would look like: