use of org.junit.jupiter.api.DynamicTest in project connect-utils by jcustenborder.
the class BaseDocumentationTest method schema.
@TestFactory
public Stream<DynamicTest> schema() throws IOException {
final File parentDirectory = new File(outputDirectory, "schemas");
if (!parentDirectory.exists()) {
parentDirectory.mkdirs();
}
List<Schema> schemas = schemas();
if (null != schemas && !schemas.isEmpty()) {
final File schemaRstPath = new File(outputDirectory, "schemas.rst");
final String schemaRst = "=======\n" + "Schemas\n" + "=======\n" + "\n" + ".. toctree::\n" + " :maxdepth: 0\n" + " :caption: Schemas:\n" + " :glob:\n" + "\n" + " schemas/*";
Files.write(schemaRst, schemaRstPath, Charsets.UTF_8);
}
final String templateName = "rst/schema.rst.ftl";
return this.schemas().stream().filter(schema -> !Strings.isNullOrEmpty(schema.name())).map(schema -> dynamicTest(String.format("%s.%s", schema.type(), schema.name()), () -> {
StringBuilder filenameBuilder = new StringBuilder().append(schema.type().toString().toLowerCase());
if (!Strings.isNullOrEmpty(schema.name())) {
filenameBuilder.append('.').append(schema.name());
}
filenameBuilder.append(".rst");
File outputFile = new File(parentDirectory, filenameBuilder.toString());
Template template = configuration.getTemplate(templateName);
log.info("Writing {}", outputFile);
try (Writer writer = Files.newWriter(outputFile, Charsets.UTF_8)) {
Map<String, Object> variables = ImmutableMap.of("input", TemplateSchema.of(schema), "helper", new RstTemplateHelper());
template.process(variables, writer);
}
}));
}
use of org.junit.jupiter.api.DynamicTest in project junit5 by junit-team.
the class ScriptTests method preconditionsAreChecked.
@TestFactory
Stream<DynamicTest> preconditionsAreChecked(TestInfo info) {
Annotation annotation = info.getTestMethod().orElseThrow(Error::new).getAnnotation(TestFactory.class);
Class<JUnitException> expected = JUnitException.class;
return //
Stream.of(//
dynamicTest("0", () -> assertNotNull(new Script(annotation, "e", "s", "r"))), //
dynamicTest("1", () -> assertThrows(expected, () -> new Script(null, "e", "s", "r"))), //
dynamicTest("2", () -> assertNotNull(new Script(Test.class, "a", "e", "s", "r"))), //
dynamicTest("3", () -> assertThrows(expected, () -> new Script(null, "a", "e", "s", "r"))), //
dynamicTest("4", () -> assertThrows(expected, () -> new Script(Test.class, null, "e", "s", "r"))), //
dynamicTest("5", () -> assertThrows(expected, () -> new Script(Test.class, "a", null, "s", "r"))), //
dynamicTest("6", () -> assertThrows(expected, () -> new Script(Test.class, "a", "e", null, "r"))), //
dynamicTest("7", () -> assertThrows(expected, () -> new Script(Test.class, "a", "e", "s", null))), //
dynamicTest("8", () -> assertNotNull(new Script(Test.class, "", "e", "s", ""))), //
dynamicTest("9", () -> assertThrows(expected, () -> new Script(Test.class, "", "", "s", ""))), //
dynamicTest("A", () -> assertThrows(expected, () -> new Script(Test.class, "", "e", "", ""))));
}
use of org.junit.jupiter.api.DynamicTest in project junit5 by junit-team.
the class DynamicTestsDemo method generateRandomNumberOfTests.
@TestFactory
Stream<DynamicTest> generateRandomNumberOfTests() {
// Generates random positive integers between 0 and 100 until
// a number evenly divisible by 7 is encountered.
Iterator<Integer> inputGenerator = new Iterator<Integer>() {
Random random = new Random();
// end::user_guide[]
{
// Use fixed seed to always produce the same number of tests for execution on the CI server
random = new Random(23);
}
// tag::user_guide[]
int current;
@Override
public boolean hasNext() {
current = random.nextInt(100);
return current % 7 != 0;
}
@Override
public Integer next() {
return current;
}
};
// Generates display names like: input:5, input:37, input:85, etc.
Function<Integer, String> displayNameGenerator = (input) -> "input:" + input;
// Executes tests based on the current input value.
ThrowingConsumer<Integer> testExecutor = (input) -> assertTrue(input % 7 != 0);
// Returns a stream of dynamic tests.
return DynamicTest.stream(inputGenerator, displayNameGenerator, testExecutor);
}
use of org.junit.jupiter.api.DynamicTest in project junit5 by junit-team.
the class TestClassWithTemplate method singleClassResolution.
@Test
void singleClassResolution() {
ClassSelector selector = selectClass(MyTestClass.class);
resolver.resolveSelectors(request().selectors(selector).build(), engineDescriptor);
assertEquals(4, engineDescriptor.getDescendants().size());
List<UniqueId> uniqueIds = uniqueIds();
assertThat(uniqueIds).contains(uniqueIdForClass(MyTestClass.class));
assertThat(uniqueIds).contains(uniqueIdForMethod(MyTestClass.class, "test1()"));
assertThat(uniqueIds).contains(uniqueIdForMethod(MyTestClass.class, "test2()"));
assertThat(uniqueIds).contains(uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()"));
}
use of org.junit.jupiter.api.DynamicTest in project junit5 by junit-team.
the class TestClassWithTemplate method twoClassesResolution.
@Test
void twoClassesResolution() {
ClassSelector selector1 = selectClass(MyTestClass.class);
ClassSelector selector2 = selectClass(YourTestClass.class);
resolver.resolveSelectors(request().selectors(selector1, selector2).build(), engineDescriptor);
assertEquals(7, engineDescriptor.getDescendants().size());
List<UniqueId> uniqueIds = uniqueIds();
assertThat(uniqueIds).contains(uniqueIdForClass(MyTestClass.class));
assertThat(uniqueIds).contains(uniqueIdForMethod(MyTestClass.class, "test1()"));
assertThat(uniqueIds).contains(uniqueIdForMethod(MyTestClass.class, "test2()"));
assertThat(uniqueIds).contains(uniqueIdForTestFactoryMethod(MyTestClass.class, "dynamicTest()"));
assertThat(uniqueIds).contains(uniqueIdForClass(YourTestClass.class));
assertThat(uniqueIds).contains(uniqueIdForMethod(YourTestClass.class, "test3()"));
assertThat(uniqueIds).contains(uniqueIdForMethod(YourTestClass.class, "test4()"));
}
Aggregations