in junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/discovery/MethodOrderingVisitor.java [66:122]
private void orderContainedMethods(ClassBasedTestDescriptor classBasedTestDescriptor, Class<?> testClass) {
findAnnotation(testClass, TestMethodOrder.class)//
.map(TestMethodOrder::value)//
.<MethodOrderer> map(ReflectionUtils::newInstance)//
.map(Optional::of)//
.orElseGet(configuration::getDefaultTestMethodOrderer)//
.ifPresent(methodOrderer -> {
Set<? extends TestDescriptor> children = classBasedTestDescriptor.getChildren();
List<TestDescriptor> nonMethodTestDescriptors = children.stream()//
.filter(testDescriptor -> !(testDescriptor instanceof MethodBasedTestDescriptor))//
.collect(Collectors.toList());
List<DefaultMethodDescriptor> methodDescriptors = children.stream()//
.filter(MethodBasedTestDescriptor.class::isInstance)//
.map(MethodBasedTestDescriptor.class::cast)//
.map(DefaultMethodDescriptor::new)//
.collect(toCollection(ArrayList::new));
// Make a local copy for later validation
Set<DefaultMethodDescriptor> originalMethodDescriptors = new LinkedHashSet<>(methodDescriptors);
methodOrderer.orderMethods(
new DefaultMethodOrdererContext(methodDescriptors, testClass, this.configuration));
int difference = methodDescriptors.size() - originalMethodDescriptors.size();
if (difference > 0) {
logger.warn(() -> String.format(
"MethodOrderer [%s] added %s MethodDescriptor(s) for test class [%s] which will be ignored.",
methodOrderer.getClass().getName(), difference, testClass.getName()));
}
else if (difference < 0) {
logger.warn(() -> String.format(
"MethodOrderer [%s] removed %s MethodDescriptor(s) for test class [%s] which will be retained with arbitrary ordering.",
methodOrderer.getClass().getName(), -difference, testClass.getName()));
}
Set<TestDescriptor> sortedMethodTestDescriptors = methodDescriptors.stream()//
.filter(originalMethodDescriptors::contains)//
.map(DefaultMethodDescriptor::getTestDescriptor)//
.collect(toCollection(LinkedHashSet::new));
// Currently no way to removeAll or addAll children at once.
Stream.concat(sortedMethodTestDescriptors.stream(), nonMethodTestDescriptors.stream())//
.forEach(classBasedTestDescriptor::removeChild);
Stream.concat(sortedMethodTestDescriptors.stream(), nonMethodTestDescriptors.stream())//
.forEach(classBasedTestDescriptor::addChild);
// Note: MethodOrderer#getDefaultExecutionMode() is guaranteed
// to be invoked after MethodOrderer#orderMethods().
methodOrderer.getDefaultExecutionMode()//
.map(JupiterTestDescriptor::toExecutionMode)//
.ifPresent(classBasedTestDescriptor::setDefaultChildExecutionMode);
});
}