Skip to content

Commit

Permalink
Document new changes
Browse files Browse the repository at this point in the history
Write new changes to the release notes for 5.11M1 and add a simple demo
in DynamicTestsDemo.

Issue: junit-team#3261
  • Loading branch information
mobounya committed Jan 20, 2024
1 parent 0930169 commit 43b708f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
Expand Up @@ -31,6 +31,9 @@ repository on GitHub.
-- for use in third-party extensions and test engines.
* Error messages for type mismatches in `NamespacedHierarchicalStore` now include the
actual type and value in addition to the required type.
* New `NamedExecutable` interface that associates a name, a payload and an Executable.
* New DynamicTests generators in DynamicTest class that takes one Stream/Iterator of NamedExecutables,
this helps simplify the generation of DynamicTests Stream.


[[release-notes-5.11.0-M1-junit-jupiter]]
Expand Down
45 changes: 44 additions & 1 deletion documentation/src/test/java/example/DynamicTestsDemo.java
Expand Up @@ -35,6 +35,7 @@
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 All @@ -44,6 +45,36 @@
// tag::user_guide[]
class DynamicTestsDemo {

static class PalindromeNamedExecutable implements NamedExecutable<PalindromeNamedExecutable> {
private final String name;
private final String payload;

public PalindromeNamedExecutable(String name, String payload) {
this.name = name;
this.payload = payload;
}

@Override
public PalindromeNamedExecutable getPayload() {
return this;
}

@Override
public String getName() {
return name;
}

@Override
public void execute() {
assertTrue(isPalindrome(getPayload().toString()));
}

@Override
public String toString() {
return payload;
}
}

private final Calculator calculator = new Calculator();

// end::user_guide[]
Expand Down Expand Up @@ -168,6 +199,19 @@ Stream<DynamicTest> dynamicTestsFromStreamFactoryMethodWithNames() {
text -> assertTrue(isPalindrome(text)));
}

@TestFactory
Stream<DynamicTest> dynamicTestsFromStreamFactoryMethodWithNamedExecutables() {
// Stream of NamedExecutables to check if a string is a palindrome
Stream<NamedExecutable<PalindromeNamedExecutable>> inputStream = Stream.of(
new PalindromeNamedExecutable("test if madam is a palindrome", "madam"),
new PalindromeNamedExecutable("another test to test mom is a palindrome", "mom"),
new PalindromeNamedExecutable("is radar a palindrome ?", "radar")
);

// Returns a stream of dynamic tests.
return DynamicTest.stream(inputStream);
}

@TestFactory
Stream<DynamicNode> dynamicTestsWithContainers() {
return Stream.of("A", "B", "C")
Expand All @@ -192,6 +236,5 @@ DynamicNode dynamicNodeSingleContainer() {
.map(text -> dynamicTest(text, () -> assertTrue(isPalindrome(text)))
));
}

}
// end::user_guide[]

0 comments on commit 43b708f

Please sign in to comment.