Skip to content

Commit

Permalink
Polish dynamic test examples in the User Guide
Browse files Browse the repository at this point in the history
  • Loading branch information
sbrannen committed Jun 4, 2016
1 parent c1f6fc7 commit f30a2a8
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
7 changes: 3 additions & 4 deletions documentation/src/docs/asciidoc/writing-tests.adoc
Expand Up @@ -310,9 +310,8 @@ however, this might be complemented by a registration facility in a later releas


The following `DynamicTestsDemo` class demonstrates several examples of test factories and dynamic tests. The following `DynamicTestsDemo` class demonstrates several examples of test factories and dynamic tests.


The first method returns an invalid return type. The first method returns an invalid return type. Since an invalid return type cannot be
Since invalid return types cannot be detected at compile-time detected at compile-time, a `JUnitException` is thrown when it is detected at run-time.
a `JUnitException` is thrown when it is detected at run-time.


The next five methods are very simple examples that demonstrate the generation of a The next five methods are very simple examples that demonstrate the generation of a
`Collection`, `Iterable`, `Iterator`, or `Stream` of `DynamicTest` instances. `Collection`, `Iterable`, `Iterator`, or `Stream` of `DynamicTest` instances.
Expand All @@ -322,7 +321,7 @@ However, `dynamicTestsFromStream()` and `dynamicTestsFromIntStream()` demonstrat
easy it is to generate dynamic tests for a given set of strings or a range of input numbers. easy it is to generate dynamic tests for a given set of strings or a range of input numbers.


The last method is truly dynamic in nature. The last method is truly dynamic in nature.
`generateRandomNumberOfTests()` implements an `Iterator` that generates random numbers, a custom `generateRandomNumberOfTests()` implements an `Iterator` that generates random numbers, a
display name generator, and a test executor and then provides all three to `DynamicTest.stream()`. display name generator, and a test executor and then provides all three to `DynamicTest.stream()`.
Although the non-deterministic behavior of `generateRandomNumberOfTests()` is of course in conflict with Although the non-deterministic behavior of `generateRandomNumberOfTests()` is of course in conflict with
test repeatability and should thus be used with care, it serves to demonstrate the expressiveness test repeatability and should thus be used with care, it serves to demonstrate the expressiveness
Expand Down
14 changes: 7 additions & 7 deletions documentation/src/test/java/example/DynamicTestsDemo.java
Expand Up @@ -87,8 +87,8 @@ Stream<DynamicTest> dynamicTestsFromStream() {
// end::user_guide[] // end::user_guide[]
// @formatter:off // @formatter:off
// tag::user_guide[] // tag::user_guide[]
return Stream.of("test1", "test2", "test3") return Stream.of("A", "B", "C").map(
.map(displayName -> dynamicTest(displayName, () -> { /* ... */ })); str -> dynamicTest("test" + str, () -> { /* ... */ }));
// end::user_guide[] // end::user_guide[]
// @formatter:on // @formatter:on
// tag::user_guide[] // tag::user_guide[]
Expand All @@ -99,8 +99,8 @@ Stream<DynamicTest> dynamicTestsFromIntStream() {
// end::user_guide[] // end::user_guide[]
// @formatter:off // @formatter:off
// tag::user_guide[] // tag::user_guide[]
return IntStream.range(1, 100).mapToObj(n -> return IntStream.range(1, 100).mapToObj(
dynamicTest("test" + n, () -> assertTrue(n % 11 != 0))); n -> dynamicTest("test" + n, () -> assertTrue(n % 11 != 0)));
// end::user_guide[] // end::user_guide[]
// @formatter:on // @formatter:on
// tag::user_guide[] // tag::user_guide[]
Expand Down Expand Up @@ -129,12 +129,12 @@ public Integer next() {
}; };


// Generates display names like: input:5, input:37, input:85, etc. // Generates display names like: input:5, input:37, input:85, etc.
Function<? super Integer, String> displayNameGenerator = (input) -> "input:" + input; Function<Integer, String> displayNameGenerator = (input) -> "input:" + input;


// Executes tests based on the current input value. // Executes tests based on the current input value.
Consumer<? super Integer> testExecutor = (input) -> assertTrue(input % 3 == 0); Consumer<Integer> testExecutor = (input) -> assertTrue(input % 3 == 0);


// Creates a stream of dynamic tests. // Returns a stream of dynamic tests.
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor); return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
} }


Expand Down

0 comments on commit f30a2a8

Please sign in to comment.