Skip to content

Commit 448f650

Browse files
committed
Use record for NamedExecutable example
1 parent c5bb451 commit 448f650

File tree

4 files changed

+79
-50
lines changed

4 files changed

+79
-50
lines changed

documentation/documentation.gradle.kts

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ tasks {
176176
}
177177
}
178178

179+
testRelease21 {
180+
include("**/*Demo.class")
181+
}
182+
179183
check {
180184
dependsOn(consoleLauncherTest)
181185
}

documentation/src/docs/asciidoc/user-guide/writing-tests.adoc

+14
Original file line numberDiff line numberDiff line change
@@ -2467,6 +2467,20 @@ a nested hierarchy of dynamic tests utilizing `DynamicContainer`.
24672467
include::{testDir}/example/DynamicTestsDemo.java[tags=user_guide]
24682468
----
24692469

2470+
[[writing-tests-dynamic-tests-named-support]]
2471+
==== Dynamic Tests and Named
2472+
2473+
In some cases, it can be more natural to specify inputs together with a descriptive name
2474+
using the {Named} API and the corresponding `stream()` factory methods on `DynamicTest` as
2475+
shown in the first example below. The second example takes it one step further and allows
2476+
to provide the code block that should be executed by implementing the `Executable`
2477+
interface along with `Named` via the `NamedExecutable` base class.
2478+
2479+
[source,java]
2480+
----
2481+
include::{testRelease21Dir}/example/DynamicTestsNamedDemo.java[tags=user_guide]
2482+
----
2483+
24702484
[[writing-tests-dynamic-tests-uri-test-source]]
24712485
==== URI Test Sources for Dynamic Tests
24722486

documentation/src/test/java/example/DynamicTestsDemo.java

+1-50
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,13 @@
1111
package example;
1212

1313
// tag::user_guide[]
14-
1514
import static example.util.StringUtils.isPalindrome;
1615
import static org.junit.jupiter.api.Assertions.assertEquals;
1716
import static org.junit.jupiter.api.Assertions.assertFalse;
1817
import static org.junit.jupiter.api.Assertions.assertNotNull;
1918
import static org.junit.jupiter.api.Assertions.assertTrue;
2019
import static org.junit.jupiter.api.DynamicContainer.dynamicContainer;
2120
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
22-
import static org.junit.jupiter.api.Named.named;
2321

2422
import java.util.Arrays;
2523
import java.util.Collection;
@@ -34,8 +32,6 @@
3432

3533
import org.junit.jupiter.api.DynamicNode;
3634
import org.junit.jupiter.api.DynamicTest;
37-
import org.junit.jupiter.api.Named;
38-
import org.junit.jupiter.api.NamedExecutable;
3935
import org.junit.jupiter.api.Tag;
4036
import org.junit.jupiter.api.TestFactory;
4137
import org.junit.jupiter.api.function.ThrowingConsumer;
@@ -102,7 +98,7 @@ Stream<DynamicTest> dynamicTestsFromIntStream() {
10298
}
10399

104100
@TestFactory
105-
Stream<DynamicTest> generateRandomNumberOfTestsFromIterator() {
101+
Stream<DynamicTest> generateRandomNumberOfTests() {
106102

107103
// Generates random positive integers between 0 and 100 until
108104
// a number evenly divisible by 7 is encountered.
@@ -154,51 +150,6 @@ Stream<DynamicTest> dynamicTestsFromStreamFactoryMethod() {
154150
return DynamicTest.stream(inputStream, displayNameGenerator, testExecutor);
155151
}
156152

157-
@TestFactory
158-
Stream<DynamicTest> dynamicTestsFromStreamFactoryMethodWithNames() {
159-
// Stream of palindromes to check
160-
Stream<Named<String>> inputStream = Stream.of(
161-
named("racecar is a palindrome", "racecar"),
162-
named("radar is also a palindrome", "radar"),
163-
named("mom also seems to be a palindrome", "mom"),
164-
named("dad is yet another palindrome", "dad")
165-
);
166-
167-
// Returns a stream of dynamic tests.
168-
return DynamicTest.stream(inputStream,
169-
text -> assertTrue(isPalindrome(text)));
170-
}
171-
172-
@TestFactory
173-
Stream<DynamicTest> dynamicTestsFromStreamFactoryMethodWithNamedExecutables() {
174-
// Stream of palindromes to check
175-
Stream<PalindromeNamedExecutable> inputStream = Stream.of("racecar", "radar", "mom", "dad")
176-
.map(PalindromeNamedExecutable::new);
177-
178-
// Returns a stream of dynamic tests based on NamedExecutables.
179-
return DynamicTest.stream(inputStream);
180-
}
181-
182-
// Can be a record in Java 16 and later
183-
static class PalindromeNamedExecutable implements NamedExecutable {
184-
185-
private final String text;
186-
187-
public PalindromeNamedExecutable(String text) {
188-
this.text = text;
189-
}
190-
191-
@Override
192-
public String getName() {
193-
return String.format("'%s' is a palindrome", text);
194-
}
195-
196-
@Override
197-
public void execute() {
198-
assertTrue(isPalindrome(text));
199-
}
200-
}
201-
202153
@TestFactory
203154
Stream<DynamicNode> dynamicTestsWithContainers() {
204155
return Stream.of("A", "B", "C")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2015-2024 the original author or authors.
3+
*
4+
* All rights reserved. This program and the accompanying materials are
5+
* made available under the terms of the Eclipse Public License v2.0 which
6+
* accompanies this distribution and is available at
7+
*
8+
* https://www.eclipse.org/legal/epl-v20.html
9+
*/
10+
11+
package example;
12+
13+
// tag::user_guide[]
14+
15+
import static example.util.StringUtils.isPalindrome;
16+
import static org.junit.jupiter.api.Assertions.assertTrue;
17+
import static org.junit.jupiter.api.Named.named;
18+
19+
import java.util.stream.Stream;
20+
21+
import org.junit.jupiter.api.DynamicTest;
22+
import org.junit.jupiter.api.NamedExecutable;
23+
import org.junit.jupiter.api.TestFactory;
24+
25+
public class DynamicTestsNamedDemo {
26+
27+
@TestFactory
28+
Stream<DynamicTest> dynamicTestsFromStreamFactoryMethodWithNames() {
29+
// Stream of palindromes to check
30+
var inputStream = Stream.of(named("racecar is a palindrome", "racecar"),
31+
named("radar is also a palindrome", "radar"), named("mom also seems to be a palindrome", "mom"),
32+
named("dad is yet another palindrome", "dad"));
33+
34+
// Returns a stream of dynamic tests.
35+
return DynamicTest.stream(inputStream, text -> assertTrue(isPalindrome(text)));
36+
}
37+
38+
@TestFactory
39+
Stream<DynamicTest> dynamicTestsFromStreamFactoryMethodWithNamedExecutables() {
40+
// Stream of palindromes to check
41+
var inputStream = Stream.of("racecar", "radar", "mom", "dad").map(PalindromeNamedExecutable::new);
42+
43+
// Returns a stream of dynamic tests based on NamedExecutables.
44+
return DynamicTest.stream(inputStream);
45+
}
46+
47+
record PalindromeNamedExecutable(String text) implements NamedExecutable {
48+
49+
@Override
50+
public String getName() {
51+
return String.format("'%s' is a palindrome", text);
52+
}
53+
54+
@Override
55+
public void execute() {
56+
assertTrue(isPalindrome(text));
57+
}
58+
}
59+
}
60+
// end::user_guide[]

0 commit comments

Comments
 (0)