diff --git a/documentation/documentation.gradle.kts b/documentation/documentation.gradle.kts index d617f773205..f5f6e1b6391 100644 --- a/documentation/documentation.gradle.kts +++ b/documentation/documentation.gradle.kts @@ -176,6 +176,10 @@ tasks { } } + testRelease21 { + include("**/*Demo.class") + } + check { dependsOn(consoleLauncherTest) } diff --git a/documentation/src/docs/asciidoc/user-guide/writing-tests.adoc b/documentation/src/docs/asciidoc/user-guide/writing-tests.adoc index 14e8f044a86..806720956ad 100644 --- a/documentation/src/docs/asciidoc/user-guide/writing-tests.adoc +++ b/documentation/src/docs/asciidoc/user-guide/writing-tests.adoc @@ -2467,6 +2467,20 @@ a nested hierarchy of dynamic tests utilizing `DynamicContainer`. include::{testDir}/example/DynamicTestsDemo.java[tags=user_guide] ---- +[[writing-tests-dynamic-tests-named-support]] +==== Dynamic Tests and Named + +In some cases, it can be more natural to specify inputs together with a descriptive name +using the {Named} API and the corresponding `stream()` factory methods on `DynamicTest` as +shown in the first example below. The second example takes it one step further and allows +to provide the code block that should be executed by implementing the `Executable` +interface along with `Named` via the `NamedExecutable` base class. + +[source,java] +---- +include::{testRelease21Dir}/example/DynamicTestsNamedDemo.java[tags=user_guide] +---- + [[writing-tests-dynamic-tests-uri-test-source]] ==== URI Test Sources for Dynamic Tests diff --git a/documentation/src/test/java/example/DynamicTestsDemo.java b/documentation/src/test/java/example/DynamicTestsDemo.java index eb31750af91..0bb2a5149cd 100644 --- a/documentation/src/test/java/example/DynamicTestsDemo.java +++ b/documentation/src/test/java/example/DynamicTestsDemo.java @@ -11,7 +11,6 @@ package example; // tag::user_guide[] - import static example.util.StringUtils.isPalindrome; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -19,7 +18,6 @@ import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.DynamicContainer.dynamicContainer; import static org.junit.jupiter.api.DynamicTest.dynamicTest; -import static org.junit.jupiter.api.Named.named; import java.util.Arrays; import java.util.Collection; @@ -34,8 +32,6 @@ import org.junit.jupiter.api.DynamicNode; import org.junit.jupiter.api.DynamicTest; -import org.junit.jupiter.api.Named; -import org.junit.jupiter.api.NamedExecutable; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.function.ThrowingConsumer; @@ -102,7 +98,7 @@ Stream dynamicTestsFromIntStream() { } @TestFactory - Stream generateRandomNumberOfTestsFromIterator() { + Stream generateRandomNumberOfTests() { // Generates random positive integers between 0 and 100 until // a number evenly divisible by 7 is encountered. @@ -154,51 +150,6 @@ Stream dynamicTestsFromStreamFactoryMethod() { return DynamicTest.stream(inputStream, displayNameGenerator, testExecutor); } - @TestFactory - Stream dynamicTestsFromStreamFactoryMethodWithNames() { - // Stream of palindromes to check - Stream> inputStream = Stream.of( - named("racecar is a palindrome", "racecar"), - named("radar is also a palindrome", "radar"), - named("mom also seems to be a palindrome", "mom"), - named("dad is yet another palindrome", "dad") - ); - - // Returns a stream of dynamic tests. - return DynamicTest.stream(inputStream, - text -> assertTrue(isPalindrome(text))); - } - - @TestFactory - Stream dynamicTestsFromStreamFactoryMethodWithNamedExecutables() { - // Stream of palindromes to check - Stream inputStream = Stream.of("racecar", "radar", "mom", "dad") - .map(PalindromeNamedExecutable::new); - - // Returns a stream of dynamic tests based on NamedExecutables. - return DynamicTest.stream(inputStream); - } - - // Can be a record in Java 16 and later - static class PalindromeNamedExecutable implements NamedExecutable { - - private final String text; - - public PalindromeNamedExecutable(String text) { - this.text = text; - } - - @Override - public String getName() { - return String.format("'%s' is a palindrome", text); - } - - @Override - public void execute() { - assertTrue(isPalindrome(text)); - } - } - @TestFactory Stream dynamicTestsWithContainers() { return Stream.of("A", "B", "C") diff --git a/documentation/src/test/java21/example/DynamicTestsNamedDemo.java b/documentation/src/test/java21/example/DynamicTestsNamedDemo.java new file mode 100644 index 00000000000..b9e2bb6cd6a --- /dev/null +++ b/documentation/src/test/java21/example/DynamicTestsNamedDemo.java @@ -0,0 +1,60 @@ +/* + * Copyright 2015-2024 the original author or authors. + * + * All rights reserved. This program and the accompanying materials are + * made available under the terms of the Eclipse Public License v2.0 which + * accompanies this distribution and is available at + * + * https://www.eclipse.org/legal/epl-v20.html + */ + +package example; + +// tag::user_guide[] + +import static example.util.StringUtils.isPalindrome; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Named.named; + +import java.util.stream.Stream; + +import org.junit.jupiter.api.DynamicTest; +import org.junit.jupiter.api.NamedExecutable; +import org.junit.jupiter.api.TestFactory; + +public class DynamicTestsNamedDemo { + + @TestFactory + Stream dynamicTestsFromStreamFactoryMethodWithNames() { + // Stream of palindromes to check + var inputStream = Stream.of(named("racecar is a palindrome", "racecar"), + named("radar is also a palindrome", "radar"), named("mom also seems to be a palindrome", "mom"), + named("dad is yet another palindrome", "dad")); + + // Returns a stream of dynamic tests. + return DynamicTest.stream(inputStream, text -> assertTrue(isPalindrome(text))); + } + + @TestFactory + Stream dynamicTestsFromStreamFactoryMethodWithNamedExecutables() { + // Stream of palindromes to check + var inputStream = Stream.of("racecar", "radar", "mom", "dad").map(PalindromeNamedExecutable::new); + + // Returns a stream of dynamic tests based on NamedExecutables. + return DynamicTest.stream(inputStream); + } + + record PalindromeNamedExecutable(String text) implements NamedExecutable { + + @Override + public String getName() { + return String.format("'%s' is a palindrome", text); + } + + @Override + public void execute() { + assertTrue(isPalindrome(text)); + } + } +} +// end::user_guide[]