Skip to content

Commit

Permalink
Revise DynamicTest support regarding display names
Browse files Browse the repository at this point in the history
Issue: #153
  • Loading branch information
sbrannen committed May 24, 2016
1 parent 32de0b4 commit 64b9481
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
37 changes: 21 additions & 16 deletions junit5-api/src/main/java/org/junit/gen5/api/DynamicTest.java
Expand Up @@ -25,12 +25,15 @@
/**
* A {@code DynamicTest} is a test case generated at runtime.
*
* <p>It is composed of a name and an {@code Executable}.
* Instances of {@code DynamicTest} must be generated by factory methods
* denoted by {@link TestFactory @TestFactory}.
* They allow for simple dynamic tests.
* Note that such tests are quite different from standard {@link Test} cases
* as the former do not execute callbacks like {@link BeforeEach} and {@link AfterEach}.
* <p>It is composed of a {@linkplain #getDisplayName display name} and an
* {@link #getExecutable Executable}.
*
* <p>Instances of {@code DynamicTest} must be generated by factory methods
* annotated with {@link TestFactory @TestFactory}.
*
* <p>Note that dynamic tests are quite different from standard {@link Test @Test}
* cases since callbacks such as {@link BeforeEach @BeforeEach} and
* {@link AfterEach @AfterEach} methods are not executed for dynamic tests.
*
* @since 5.0
* @see Test
Expand All @@ -40,31 +43,33 @@
@API(Experimental)
public class DynamicTest {

private final String name;
private final String displayName;
private final Executable executable;

public DynamicTest(String name, Executable executable) {
this.name = name;
public DynamicTest(String displayName, Executable executable) {
this.displayName = displayName;
this.executable = executable;
}

public String getName() {
return name;
public String getDisplayName() {
return this.displayName;
}

public Executable getExecutable() {
return executable;
return this.executable;
}

public static <T extends Object> Stream<DynamicTest> streamFrom(Iterator<T> generator,
Function<T, String> nameSupplier, Consumer<T> assertion) {
Function<T, String> displayNameSupplier, Consumer<T> assertion) {

Stream<T> targetStream = StreamSupport.stream(
Spliterators.spliteratorUnknownSize(generator, Spliterator.ORDERED), false);

return targetStream.map(element -> {
String testName = nameSupplier.apply(element);
String displayName = displayNameSupplier.apply(element);
Executable testExecutable = () -> assertion.accept(element);
return new DynamicTest(testName, testExecutable);
return new DynamicTest(displayName, testExecutable);
});

}

}
Expand Up @@ -35,14 +35,9 @@ public DynamicTestTestDescriptor(UniqueId uniqueId, DynamicTest dynamicTest, Tes
setSource(source);
}

@Override
public String getName() {
return dynamicTest.getName();
}

@Override
public String getDisplayName() {
return dynamicTest.getName();
return this.dynamicTest.getDisplayName();
}

@Override
Expand All @@ -54,4 +49,5 @@ public boolean isTest() {
public boolean isContainer() {
return false;
}

}
Expand Up @@ -69,7 +69,7 @@ protected void invokeTestMethod(JUnit5EngineExecutionContext context, TestExtens

throwableCollector.execute(() -> {
MethodInvocationContext methodInvocationContext = methodInvocationContext(
testExtensionContext.getTestInstance(), testExtensionContext.getTestMethod());
testExtensionContext.getTestInstance(), testExtensionContext.getTestMethod().get());

MethodInvoker methodInvoker = new MethodInvoker(testExtensionContext, context.getExtensionRegistry());
Object testFactoryMethodResult = methodInvoker.invoke(methodInvocationContext);
Expand Down
Expand Up @@ -22,8 +22,8 @@
import org.junit.gen5.engine.junit5.descriptor.TestFactoryTestDescriptor;

/**
* {@code TestFactoryMethodResolver} is a special {@link ElementResolver}
* which is able to resolve test factory methods denoted by
* {@code TestFactoryMethodResolver} is an {@link ElementResolver}
* that is able to resolve test factory methods annotated with
* {@link TestFactory @TestFactory}.
*
* <p>It will create {@link TestFactoryTestDescriptor} instances.
Expand All @@ -44,10 +44,12 @@ public TestFactoryMethodResolver() {
super(SEGMENT_TYPE);
}

@Override
protected boolean isTestMethod(Method candidate) {
return isTestFactoryMethod.test(candidate);
}

@Override
protected TestDescriptor resolveMethod(Method testMethod, ClassTestDescriptor parentClassDescriptor,
UniqueId uniqueId) {
return new TestFactoryTestDescriptor(uniqueId, parentClassDescriptor.getTestClass(), testMethod);
Expand Down

0 comments on commit 64b9481

Please sign in to comment.