Skip to content

Commit

Permalink
Use record for NamedExecutable example
Browse files Browse the repository at this point in the history
  • Loading branch information
marcphilipp committed Aug 10, 2024
1 parent c5bb451 commit 448f650
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 50 deletions.
4 changes: 4 additions & 0 deletions documentation/documentation.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ tasks {
}
}

testRelease21 {
include("**/*Demo.class")
}

check {
dependsOn(consoleLauncherTest)
}
Expand Down
14 changes: 14 additions & 0 deletions documentation/src/docs/asciidoc/user-guide/writing-tests.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
51 changes: 1 addition & 50 deletions documentation/src/test/java/example/DynamicTestsDemo.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@
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;
import static org.junit.jupiter.api.Assertions.assertNotNull;
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;
Expand All @@ -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;
Expand Down Expand Up @@ -102,7 +98,7 @@ Stream<DynamicTest> dynamicTestsFromIntStream() {
}

@TestFactory
Stream<DynamicTest> generateRandomNumberOfTestsFromIterator() {
Stream<DynamicTest> generateRandomNumberOfTests() {

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

@TestFactory
Stream<DynamicTest> dynamicTestsFromStreamFactoryMethodWithNames() {
// Stream of palindromes to check
Stream<Named<String>> 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<DynamicTest> dynamicTestsFromStreamFactoryMethodWithNamedExecutables() {
// Stream of palindromes to check
Stream<PalindromeNamedExecutable> 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<DynamicNode> dynamicTestsWithContainers() {
return Stream.of("A", "B", "C")
Expand Down
60 changes: 60 additions & 0 deletions documentation/src/test/java21/example/DynamicTestsNamedDemo.java
Original file line number Diff line number Diff line change
@@ -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<DynamicTest> 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<DynamicTest> 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[]

0 comments on commit 448f650

Please sign in to comment.