Top Description Inners Fields Methods
org.junit.jupiter.api

public Interface MethodOrderer

Known Direct Implementers
org.junit.jupiter.api.MethodOrderer.Default, org.junit.jupiter.api.MethodOrderer.MethodName, org.junit.jupiter.api.MethodOrderer.DisplayName, org.junit.jupiter.api.MethodOrderer.OrderAnnotation, org.junit.jupiter.api.MethodOrderer.Random
Annotations
@API
status:STABLE
since:5.7
Static Imports
java.util.Comparator.comparingInt, org.apiguardian.api.API.Status.EXPERIMENTAL, .API.Status.STABLE

MethodOrderer defines the API for ordering the test methods in a given test class.

In this context, the term "test method" refers to any method annotated with @Test, @RepeatedTest, @ParameterizedTest, @TestFactory, or @TestTemplate.

A MethodOrderer can be configured globally for the entire test suite via the junit.jupiter.testmethod.order.default configuration parameter (see the User Guide for details) or locally for a test class via the @TestMethodOrder annotation.

Built-in Implementations

JUnit Jupiter provides the following built-in MethodOrderer implementations.

Since
5.4
See Also
TestMethodOrder, MethodOrdererContext, orderMethods(MethodOrdererContext), ClassOrderer

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static class
MethodOrderer.Default

MethodOrderer that allows to explicitly specify that the default ordering should be applied.

public static class
MethodOrderer.DisplayName

MethodOrderer that sorts methods alphanumerically based on their display names using String#compareTo(String)

public static class
MethodOrderer.MethodName

MethodOrderer that sorts methods alphanumerically based on their names using String#compareTo(String).

public static class
MethodOrderer.OrderAnnotation

MethodOrderer that sorts methods based on the @Order annotation.

public static class
MethodOrderer.Random

MethodOrderer that orders methods pseudo-randomly.

Field Summary

Modifier and TypeField and Description
public static final String
DEFAULT_ORDER_PROPERTY_NAME

Property name used to set the default method orderer class name: Supported Values

Supported values include fully qualified class names for types that implement org.junit.jupiter.api.MethodOrderer.

Method Summary

Modifier and TypeMethod and Description
public default Optional<ExecutionMode>

Returns:

the default ExecutionMode; never null but potentially empty
getDefaultExecutionMode
()

Get the default ExecutionMode for the test class configured with this MethodOrderer.

public void
orderMethods(MethodOrdererContext
the MethodOrdererContext containing the method descriptors to order; never null
context
)

Order the methods encapsulated in the supplied MethodOrdererContext.

Field Detail

DEFAULT_ORDER_PROPERTY_NAMEback to summary
public static final String DEFAULT_ORDER_PROPERTY_NAME

Property name used to set the default method orderer class name:

Supported Values

Supported values include fully qualified class names for types that implement org.junit.jupiter.api.MethodOrderer.

If not specified, test methods will be ordered using an algorithm that is deterministic but intentionally non-obvious.

Annotations
@API
status:STABLE
since:5.9
Since
5.7

Method Detail

getDefaultExecutionModeback to summary
public default Optional<ExecutionMode> getDefaultExecutionMode()

Get the default ExecutionMode for the test class configured with this MethodOrderer.

This method is guaranteed to be invoked after orderMethods(MethodOrdererContext) which allows implementations of this method to determine the appropriate return value programmatically, potentially based on actions that were taken in orderMethods().

Defaults to SAME_THREAD, since ordered methods are typically sorted in a fashion that would conflict with concurrent execution.

In case the ordering does not conflict with concurrent execution, implementations should return an empty Optional to signal that the engine should decide which execution mode to use.

Can be overridden via an explicit @Execution declaration on the test class or in concrete implementations of the MethodOrderer API.

Returns:Optional<ExecutionMode>

the default ExecutionMode; never null but potentially empty

See Also
orderMethods(MethodOrdererContext)
orderMethodsback to summary
public void orderMethods(MethodOrdererContext context)

Order the methods encapsulated in the supplied MethodOrdererContext.

The methods to order or sort are made indirectly available via MethodOrdererContext#getMethodDescriptors(). Since this method has a void return type, the list of method descriptors must be modified directly.

For example, a simplified implementation of the Random MethodOrderer might look like the following.

public void orderMethods(MethodOrdererContext context) {
    Collections.shuffle(context.getMethodDescriptors());
}
Parameters
context:MethodOrdererContext

the MethodOrdererContext containing the method descriptors to order; never null

See Also
getDefaultExecutionMode()
org.junit.jupiter.api back to summary

public final Class MethodOrderer.Default

extends Object
implements MethodOrderer
Class Inheritance
All Implemented Interfaces
org.junit.jupiter.api.MethodOrderer
Annotations
@API
status:EXPERIMENTAL
since:6.0

MethodOrderer that allows to explicitly specify that the default ordering should be applied.

If the junit.jupiter.testmethod.order.default is set, specifying this MethodOrderer has the same effect as referencing the configured class directly. Otherwise, it has the same effect as not specifying any MethodOrderer.

This class can be used to reset the MethodOrderer for a @Nested class and its @Nested inner classes, recursively, when a MethodOrderer is configured using @TestMethodOrder on an enclosing class.

Since
6.0

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public void
orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Order the methods encapsulated in the supplied MethodOrdererContext.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

Defaultback to summary
private Default()

Method Detail

orderMethodsback to summary
public void orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Doc from org.junit.jupiter.api.MethodOrderer.orderMethods.

Order the methods encapsulated in the supplied MethodOrdererContext.

The methods to order or sort are made indirectly available via MethodOrdererContext#getMethodDescriptors(). Since this method has a void return type, the list of method descriptors must be modified directly.

For example, a simplified implementation of the Random MethodOrderer might look like the following.

public void orderMethods(MethodOrdererContext context) {
    Collections.shuffle(context.getMethodDescriptors());
}
Annotations
@Override
org.junit.jupiter.api back to summary

public Class MethodOrderer.DisplayName

extends Object
implements MethodOrderer
Class Inheritance
All Implemented Interfaces
org.junit.jupiter.api.MethodOrderer
Annotations
@API
status:STABLE
since:5.10

MethodOrderer that sorts methods alphanumerically based on their display names using String#compareTo(String)
Since
5.7

Field Summary

Modifier and TypeField and Description
private static final Comparator<MethodDescriptor>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Sort the methods encapsulated in the supplied MethodOrdererContext alphanumerically based on their display names.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

comparatorback to summary
private static final Comparator<MethodDescriptor> comparator

Constructor Detail

DisplayNameback to summary
public DisplayName()

Method Detail

orderMethodsback to summary
public void orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Sort the methods encapsulated in the supplied MethodOrdererContext alphanumerically based on their display names.

Annotations
@Override
org.junit.jupiter.api back to summary

public Class MethodOrderer.MethodName

extends Object
implements MethodOrderer
Class Inheritance
All Implemented Interfaces
org.junit.jupiter.api.MethodOrderer
Annotations
@API
status:STABLE
since:5.10

MethodOrderer that sorts methods alphanumerically based on their names using String#compareTo(String).

If two methods have the same name, String representations of their formal parameter lists will be used as a fallback for comparing the methods.

Since
5.7

Field Summary

Modifier and TypeField and Description
private static final Comparator<MethodDescriptor>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Sort the methods encapsulated in the supplied MethodOrdererContext alphanumerically based on their names and formal parameter lists.

private static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

comparatorback to summary
private static final Comparator<MethodDescriptor> comparator

Constructor Detail

MethodNameback to summary
public MethodName()

Method Detail

orderMethodsback to summary
public void orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Sort the methods encapsulated in the supplied MethodOrdererContext alphanumerically based on their names and formal parameter lists.

Annotations
@Override
parameterListback to summary
private static String parameterList(Method method)
org.junit.jupiter.api back to summary

public Class MethodOrderer.OrderAnnotation

extends Object
implements MethodOrderer
Class Inheritance
All Implemented Interfaces
org.junit.jupiter.api.MethodOrderer

MethodOrderer that sorts methods based on the @Order annotation.

Any methods that are assigned the same order value will be sorted arbitrarily adjacent to each other.

Any methods not annotated with @Order will be assigned the default order value which will effectively cause them to appear at the end of the sorted list, unless certain methods are assigned an explicit order value greater than the default order value. Any methods assigned an explicit order value greater than the default order value will appear after non-annotated methods in the sorted list.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private static int
public void
orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Sort the methods encapsulated in the supplied MethodOrdererContext based on the @Order annotation.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

OrderAnnotationback to summary
public OrderAnnotation()

Method Detail

getOrderback to summary
private static int getOrder(MethodDescriptor descriptor)
orderMethodsback to summary
public void orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Sort the methods encapsulated in the supplied MethodOrdererContext based on the @Order annotation.

Annotations
@Override
org.junit.jupiter.api back to summary

public Class MethodOrderer.Random

extends Object
implements MethodOrderer
Class Inheritance
All Implemented Interfaces
org.junit.jupiter.api.MethodOrderer

MethodOrderer that orders methods pseudo-randomly.

Custom Seed

By default, the random seed used for ordering methods is the value returned by System#nanoTime() during static class initialization. In order to support repeatable builds, the value of the default random seed is logged at CONFIG level. In addition, a custom seed (potentially the default seed from the previous test plan execution) may be specified via the junit.jupiter.execution.order.random.seed configuration parameter which can be supplied via the Launcher API, build tools (e.g., Gradle and Maven), a JVM system property, or the JUnit Platform configuration file (i.e., a file named junit-platform.properties in the root of the class path). Consult the User Guide for further information.

See Also
Random#RANDOM_SEED_PROPERTY_NAME, java.util.Random

Field Summary

Modifier and TypeField and Description
private static final Logger
public static final String
RANDOM_SEED_PROPERTY_NAME

Property name used to set the random seed used by this MethodOrderer:

The same property is used by ClassOrderer.Random for consistency between the two random orderers.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Order the methods encapsulated in the supplied MethodOrdererContext pseudo-randomly.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

loggerback to summary
private static final Logger logger
RANDOM_SEED_PROPERTY_NAMEback to summary
public static final String RANDOM_SEED_PROPERTY_NAME

Property name used to set the random seed used by this MethodOrderer:

The same property is used by ClassOrderer.Random for consistency between the two random orderers.

Supported Values

Supported values include any string that can be converted to a Long via Long#valueOf(String).

If not specified or if the specified value cannot be converted to a Long, the default random seed will be used (see the class-level Javadoc for details).

See Also
ClassOrderer.Random

Constructor Detail

Randomback to summary
public Random()

Method Detail

orderMethodsback to summary
public void orderMethods(MethodOrdererContext context)

Implements org.junit.jupiter.api.MethodOrderer.orderMethods.

Order the methods encapsulated in the supplied MethodOrdererContext pseudo-randomly.

Annotations
@Override