Tuomas Toivonen Tuomas Toivonen - 1 year ago 90
Java Question

How to run two set of tests with maven with delay in between

Our goal is to make acceptance test for a system, which can be disabled for a specified period, and ensure system gets disabled and back to enabled.

Our plan is to:

  • first disable the system for X minutes from maintenance client using Selenium

  • then run Java based (or other) test to query the system, and assert the system is disabled

  • then we should sleep for X minutes and run another test to ensure system is enabled again.

We like to keep the tests for enabled and disabled cases decoupled, thus the sleep must be introduced either in maven itself, or as some plugin type solution.

Question: How to specify in which order the test goals are ran by maven, and add parameterized delay in between, which will be fed to Selenium?

Answer Source

You could apply the following configuration:

  • Configure the maven-surefire-plugin or the maven-failsafe-plugin (better for integration tests, which sounds more appropriate in this use case) to execute the first set of tests via its include/exclude mechanism, as first execution of this plugin
  • Configure the maven-surefire-plugin (or the maven-failsafe-plugin) to execute a sample test case whose only purpose would be to sleep for a determined (or configurable) time, again via include/exclude mechanism, as second execution of this plugin (Maven will respect execution order as per pom.xml file declaration)
  • Configure the maven-surefire-plugin (or the maven-failsafe-plugin) to execute the second set of tests (or a single check test, in this use case), again via include/exclude, as third execution (which will then be executed as last one).

Using the same plugin with several executions for the same phase will ensure you the declaration order will be followed during Maven execution.

Below a sample snippet of the approach above:



Note, I wrapped everything in a Maven profile for cleanness since you probably don't want this behavior as part of your default build but only executed on demand (or as part of a CI job).

If you need to configure the sleeping time, you could then configure the concerned configuration section of each execution via the systemPropertyVariables option.

You can then invoke your build as following:

mvn clean verify -Pcheck-test -Dsleep.time=3000

Where the -P is enabling the profile by its id and we are also overriding the default value of the sleep.time property via command line, passed then as value of the sleepParam system variable which can be fetched from Java code via a System.gerProperty("sleepParam") call.

Also note, the maven-failsafe-plugin may better suit your scenario since it better handles post-execution of integration/acceptance tests, as also described in its official page, even though your use case may still be served by the `maven-surefire-plugin'.