Skip to content

Commit

Permalink
#58 Dynamic test method can now return iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Mar 4, 2016
1 parent 148f533 commit c3da97f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -99,6 +116,16 @@ Collection<DynamicTest> dynamicCollection() {
return tests;
}

@Dynamic
Iterator<DynamicTest> dynamicIterator() {
List<DynamicTest> tests = new ArrayList<>();

tests.add(new DynamicTest("succeedingTest", () -> Assertions.assertTrue(true, "succeeding")));
tests.add(new DynamicTest("failingTest", () -> Assertions.assertTrue(false, "failing")));

return tests.iterator();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -78,6 +82,11 @@ private Stream<DynamicTest> toDynamicTestStream(Object dynamicMethodResult) {
Collection<DynamicTest> dynamicTestCollection = (Collection<DynamicTest>) dynamicMethodResult;
return dynamicTestCollection.stream();
}
if (dynamicMethodResult instanceof Iterator) {
Iterator<DynamicTest> dynamicTestIterator = (Iterator<DynamicTest>) dynamicMethodResult;
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(dynamicTestIterator, Spliterator.ORDERED), false);
}

}
catch (ClassCastException cce) {
Expand Down

0 comments on commit c3da97f

Please sign in to comment.