Skip to content

Commit 4f9405e

Browse files
committed
Document URI TestSource support of dynamic tests in User Guide
1 parent 7c7977c commit 4f9405e

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,28 @@ include::{testDir}/example/DynamicTestsDemo.java[tags=user_guide]
15711571
----
15721572

15731573

1574+
[[writing-tests-uris-test-source-examples]]
1575+
==== URI Test Source Examples
1576+
1577+
The JUnit Platform provide `TestSource`, a representation of the source of a test
1578+
or container used to navigate to its location by IDEs and build tools.
1579+
1580+
Some test sources can be constructed using URIs :
1581+
1582+
- `ClasspathResourceSource` : uses the `classpath` scheme ex `classpath:/test/foo.xml?line=20,column=2`
1583+
- `DirectorySource` and `FileSource` : uses filesystem paths
1584+
- `MethodSource` : uses the `method` scheme and the fully-qualified-method-name ex
1585+
`method:org.junit.Foo#bar(java.lang.String, java.lang.String[])`. Please refer to the Javadoc for
1586+
`DiscoverySelectors.selectMethod(String)` for the supported formats for a FQMN.
1587+
1588+
The following `TestSourceUrisDemo` class
1589+
demonstrates several examples of dynamic tests using test source URIs.
1590+
1591+
[source,java]
1592+
----
1593+
include::{testDir}/example/TestSourceUrisDemo.java[tags=user_guide]
1594+
----
1595+
15741596
[[writing-tests-declarative-timeouts]]
15751597
=== Timeouts
15761598

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2015-2019 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 org.junit.jupiter.api.Assertions.assertTrue;
16+
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
17+
18+
import java.io.File;
19+
import java.net.URI;
20+
import java.nio.file.Paths;
21+
22+
import org.junit.jupiter.api.DynamicTest;
23+
import org.junit.jupiter.api.TestFactory;
24+
25+
// end::user_guide[]
26+
// @formatter:off
27+
// tag::user_guide[]
28+
public class TestSourceUrisDemo {
29+
30+
@TestFactory
31+
DynamicTest classpathResourceSource() {
32+
return dynamicTest(
33+
"class path resource source uri",
34+
URI.create("classpath:/two-column.csv"), // test source uri
35+
() -> assertTrue(true)); // for demo purpose
36+
}
37+
38+
@TestFactory
39+
DynamicTest directorySource() {
40+
File directory = Paths.get("src/test/resources").toFile();
41+
return dynamicTest("directory source uri",
42+
directory.toURI(), // test source uri
43+
() -> assertTrue(true)); // for demo purpose
44+
}
45+
46+
// Inspired by https://sormuras.github.io/blog/2018-09-05-junit-5.3-dynamic-test-source.html
47+
@TestFactory
48+
DynamicTest fileSource() {
49+
File file = Paths.get("src/test/resources/two-column.csv").toFile();
50+
return dynamicTest(
51+
file.getName(),
52+
file.toURI(), // test source uri
53+
() -> assertTrue(true)); // for demo purpose
54+
}
55+
56+
@TestFactory
57+
DynamicTest methodSource() {
58+
return dynamicTest("method source uri",
59+
URI.create("method:example.TestSourceUrisDemo#method()"), // test source uri
60+
() -> assertTrue(true)); // for demo purpose
61+
}
62+
63+
private void method() {
64+
}
65+
}
66+
// end::user_guide[]

0 commit comments

Comments
 (0)