diff --git a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java index 78b9f7ea8809..1467882e5fad 100644 --- a/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java +++ b/junit-jupiter-engine/src/main/java/org/junit/jupiter/engine/descriptor/TestTemplateTestDescriptor.java @@ -14,8 +14,9 @@ import static org.junit.platform.commons.meta.API.Usage.Internal; import java.lang.reflect.Method; +import java.util.Iterator; import java.util.List; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.stream.Stream; import org.junit.jupiter.api.extension.ContainerExtensionContext; import org.junit.jupiter.api.extension.TestTemplateInvocationContext; @@ -89,13 +90,18 @@ public JupiterEngineExecutionContext execute(JupiterEngineExecutionContext conte ContainerExtensionContext containerExtensionContext = (ContainerExtensionContext) context.getExtensionContext(); List providers = validateProviders(containerExtensionContext, context.getExtensionRegistry()); - AtomicInteger invocationIndex = new AtomicInteger(); - // @formatter:off - providers.stream() - .flatMap(provider -> provider.provide(containerExtensionContext)) - .map(invocationContext -> createInvocationTestDescriptor(invocationContext, invocationIndex.incrementAndGet())) - .forEach(invocationTestDescriptor -> execute(dynamicTestExecutor, invocationTestDescriptor)); - // @formatter:on + + int invocationIndex = 0; + for (TestTemplateInvocationContextProvider provider : providers) { + Stream invocationContextStream = provider.provide(containerExtensionContext); + Iterator contextIterator = invocationContextStream.iterator(); + while (contextIterator.hasNext()) { + TestDescriptor invocationTestDescriptor = createInvocationTestDescriptor(contextIterator.next(), + ++invocationIndex); + execute(dynamicTestExecutor, invocationTestDescriptor); + } + invocationContextStream.close(); + } validateWasAtLeastInvokedOnce(invocationIndex); return context; } @@ -127,8 +133,8 @@ private void execute(DynamicTestExecutor dynamicTestExecutor, TestDescriptor tes dynamicTestExecutor.execute(testDescriptor); } - private void validateWasAtLeastInvokedOnce(AtomicInteger invocationIndex) { - if (invocationIndex.get() == 0) { + private void validateWasAtLeastInvokedOnce(int invocationIndex) { + if (invocationIndex == 0) { throw new TestAbortedException("No supporting " + TestTemplateInvocationContextProvider.class.getSimpleName() + " provided an invocation context"); }