Skip to content

Execution Order for JUnit Tests #2948

Answered by sbrannen
GreihMurray asked this question in Q&A
Discussion options

You must be logged in to vote

JUnit introspects the methods of a compiled test class using Java's reflection APIs. JUnit is not aware of the source code of test classes.

The JDK does not provide any API that allows one to infer the order in which methods are declared in the source code.

The only built-in tool we have is java.lang.Class.getDeclaredMethods() which makes no guarantee about the ordering of the methods.

Thus, in order to enforce consistent ordering of test methods across multiple runs of the same test suite, we implemented a mechanism that is non-obvious yet deterministic, as stated in the User Guide:

By default, test classes and methods will be ordered using an algorithm that is deterministic but intenti…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@GreihMurray
Comment options

Answer selected by sbrannen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants