|
| 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