@API(status=EXPERIMENTAL,
since="5.4")
public interface MethodOrderer
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
.
JUnit Jupiter provides the following built-in MethodOrderer
implementations.
TestMethodOrder
,
MethodOrdererContext
,
orderMethods(MethodOrdererContext)
Modifier and Type | Interface and Description |
---|---|
static class |
MethodOrderer.Alphanumeric
MethodOrderer that sorts methods alphanumerically based on their
names using String.compareTo(String) . |
static class |
MethodOrderer.OrderAnnotation
MethodOrderer that sorts methods based on the @Order
annotation. |
static class |
MethodOrderer.Random
MethodOrderer that orders methods pseudo-randomly and allows for
concurrent execution by default. |
Modifier and Type | Method and Description |
---|---|
default Optional<ExecutionMode> |
getDefaultExecutionMode()
Get the default
ExecutionMode for the test class
configured with this MethodOrderer . |
void |
orderMethods(MethodOrdererContext context)
Order the methods encapsulated in the supplied
MethodOrdererContext . |
void orderMethods(MethodOrdererContext context)
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 MethodOrderer.Random
MethodOrderer
might look like the following.
public void orderMethods(MethodOrdererContext context) { Collections.shuffle(context.getMethodDescriptors()); }
context
- the MethodOrdererContext
containing the
method descriptors
to order; never null
getDefaultExecutionMode()
default Optional<ExecutionMode> getDefaultExecutionMode()
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.
ExecutionMode
; never null
but
potentially emptyorderMethods(MethodOrdererContext)
Copyright © 2021. All rights reserved.