With Java 1.6 or prior, JUnit tests are executed in the order in which they are declared in the source file.
Note: With Java 1.7 or later, you cannot specify the order in which the JUnit tests are executed. This is a JUnit limitation for
JUnit tests are executed differently, depending on the JUnit version. With a JUnit version prior to 4.11 the tests are executed
in no particular order, which may differ between test runs. With JUnit 4.11 or higher the tests are executed in the same order
for each test run, but the order is unpredictable.
Depending on your testing environment you might be able to workaround this limitation.
Examples for a workaround
If your test set does not include modules and suites, you could add the following lines to the start of the source file:
There are three possible values you can specify for the
- The order in which the methods are returned by the JVM, potentially a different order for each test run. Might break your
- Deterministic ordering based upon the hashCode of the method name. Changing the order is difficult because you have to define
method names that lead to an appropriate hashCode.
- The order is based upon the lexicographic ordering of the names of the tests. You would have to rename your tests so that
the alphabetical order of the test names matches the order in which you want the tests to be executed.
You could also use a Java version prior to 1.7.