-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use record for NamedExecutable example
- Loading branch information
1 parent
c5bb451
commit 448f650
Showing
4 changed files
with
79 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,6 +176,10 @@ tasks { | |
} | ||
} | ||
|
||
testRelease21 { | ||
include("**/*Demo.class") | ||
} | ||
|
||
check { | ||
dependsOn(consoleLauncherTest) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
documentation/src/test/java21/example/DynamicTestsNamedDemo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |