From c3da97f3fe7fffab893ab41cc99bc8693b5a8715 Mon Sep 17 00:00:00 2001 From: Johannes Link Date: Fri, 4 Mar 2016 09:15:19 +0100 Subject: [PATCH] #58 Dynamic test method can now return iterator --- .../junit5/DynamicTestGenerationTests.java | 29 ++++++++++++++++++- .../DynamicMethodTestDescriptor.java | 9 ++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/junit-tests/src/test/java/org/junit/gen5/engine/junit5/DynamicTestGenerationTests.java b/junit-tests/src/test/java/org/junit/gen5/engine/junit5/DynamicTestGenerationTests.java index f84a07d62ad9..bb7393dd257e 100644 --- a/junit-tests/src/test/java/org/junit/gen5/engine/junit5/DynamicTestGenerationTests.java +++ b/junit-tests/src/test/java/org/junit/gen5/engine/junit5/DynamicTestGenerationTests.java @@ -17,6 +17,7 @@ import java.util.ArrayList; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.stream.Stream; @@ -34,7 +35,7 @@ class DynamicTestGenerationTests extends AbstractJUnit5TestEngineTests { public void dynamicTestMethodsAreCorrectlyDiscoveredForClassSelector() { TestDiscoveryRequest request = request().select(forClass(MyDynamicTestCase.class)).build(); TestDescriptor engineDescriptor = discoverTests(request); - assertEquals(3, engineDescriptor.allDescendants().size(), "# resolved test descriptors"); + assertEquals(4, engineDescriptor.allDescendants().size(), "# resolved test descriptors"); } @Test @@ -77,6 +78,22 @@ public void dynamicTestsAreExecutedFromCollection() { () -> assertEquals(3L, eventRecorder.getContainerFinishedCount(), "# container finished")); } + @Test + public void dynamicTestsAreExecutedFromIterator() { + TestDiscoveryRequest request = request().select(forMethod(MyDynamicTestCase.class, "dynamicIterator")).build(); + + ExecutionEventRecorder eventRecorder = executeTests(request); + + //dynamic test methods are counted as both container and test + assertAll( // + () -> assertEquals(3L, eventRecorder.getContainerStartedCount(), "# container started"), + () -> assertEquals(2L, eventRecorder.getDynamicTestRegisteredCount(), "# dynamic registered"), + () -> assertEquals(3L, eventRecorder.getTestStartedCount(), "# tests started"), + () -> assertEquals(2L, eventRecorder.getTestSuccessfulCount(), "# tests succeeded"), + () -> assertEquals(1L, eventRecorder.getTestFailedCount(), "# tests failed"), + () -> assertEquals(3L, eventRecorder.getContainerFinishedCount(), "# container finished")); + } + private static class MyDynamicTestCase { @Dynamic @@ -99,6 +116,16 @@ Collection dynamicCollection() { return tests; } + @Dynamic + Iterator dynamicIterator() { + List tests = new ArrayList<>(); + + tests.add(new DynamicTest("succeedingTest", () -> Assertions.assertTrue(true, "succeeding"))); + tests.add(new DynamicTest("failingTest", () -> Assertions.assertTrue(false, "failing"))); + + return tests.iterator(); + } + } } diff --git a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/DynamicMethodTestDescriptor.java b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/DynamicMethodTestDescriptor.java index 78817c7cfa8b..10773cc86e6b 100644 --- a/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/DynamicMethodTestDescriptor.java +++ b/junit5-engine/src/main/java/org/junit/gen5/engine/junit5/descriptor/DynamicMethodTestDescriptor.java @@ -15,8 +15,12 @@ import java.lang.reflect.Method; import java.util.Collection; +import java.util.Iterator; +import java.util.Spliterator; +import java.util.Spliterators; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; +import java.util.stream.StreamSupport; import org.junit.gen5.api.DynamicTest; import org.junit.gen5.api.extension.MethodInvocationContext; @@ -78,6 +82,11 @@ private Stream toDynamicTestStream(Object dynamicMethodResult) { Collection dynamicTestCollection = (Collection) dynamicMethodResult; return dynamicTestCollection.stream(); } + if (dynamicMethodResult instanceof Iterator) { + Iterator dynamicTestIterator = (Iterator) dynamicMethodResult; + return StreamSupport.stream( + Spliterators.spliteratorUnknownSize(dynamicTestIterator, Spliterator.ORDERED), false); + } } catch (ClassCastException cce) {