Search in sources :

Example 1 with DynamicNode

use of org.junit.jupiter.api.DynamicNode in project junit5 by junit-team.

the class TestFactoryTestDescriptor method invokeTestMethod.

// --- Node ----------------------------------------------------------------
@Override
protected void invokeTestMethod(JupiterEngineExecutionContext context, DynamicTestExecutor dynamicTestExecutor) {
    ExtensionContext extensionContext = context.getExtensionContext();
    context.getThrowableCollector().execute(() -> {
        Object instance = extensionContext.getRequiredTestInstance();
        Object testFactoryMethodResult = executableInvoker.invoke(getTestMethod(), instance, extensionContext, context.getExtensionRegistry());
        TestSource source = getSource().orElseThrow(() -> new JUnitException("Illegal state: TestSource must be present"));
        try (Stream<DynamicNode> dynamicNodeStream = toDynamicNodeStream(testFactoryMethodResult)) {
            int index = 1;
            Iterator<DynamicNode> iterator = dynamicNodeStream.iterator();
            while (iterator.hasNext()) {
                DynamicNode dynamicNode = iterator.next();
                Optional<JupiterTestDescriptor> descriptor = createDynamicDescriptor(this, dynamicNode, index++, source, getDynamicDescendantFilter());
                descriptor.ifPresent(dynamicTestExecutor::execute);
            }
        } catch (ClassCastException ex) {
            throw invalidReturnTypeException(ex);
        }
    });
}
Also used : TestSource(org.junit.platform.engine.TestSource) JUnitException(org.junit.platform.commons.JUnitException) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) DynamicNode(org.junit.jupiter.api.DynamicNode)

Example 2 with DynamicNode

use of org.junit.jupiter.api.DynamicNode in project junit5 by junit-team.

the class ConsoleDetailsTests method scanContainerClassAndCreateDynamicTests.

private List<DynamicNode> scanContainerClassAndCreateDynamicTests(Class<?> containerClass) {
    String containerName = containerClass.getSimpleName().replace("TestCase", "");
    // String containerName = containerClass.getSimpleName();
    List<DynamicNode> nodes = new ArrayList<>();
    Map<Details, List<DynamicTest>> map = new EnumMap<>(Details.class);
    for (Method method : findMethods(containerClass, m -> m.isAnnotationPresent(Test.class))) {
        String methodName = method.getName();
        Class<?>[] types = method.getParameterTypes();
        for (Details details : Details.values()) {
            List<DynamicTest> tests = map.computeIfAbsent(details, key -> new ArrayList<>());
            for (Theme theme : Theme.values()) {
                String caption = containerName + "-" + methodName + "-" + details + "-" + theme;
                String[] args = { // 
                "--include-engine", // 
                "junit-jupiter", // 
                "--details", // 
                details.name(), // 
                "--details-theme", // 
                theme.name(), // 
                "--disable-ansi-colors", // 
                "true", // 
                "--include-classname", // 
                containerClass.getCanonicalName(), // 
                "--select-method", // 
                getFullyQualifiedMethodName(containerClass, methodName, types) };
                String displayName = methodName + "() " + theme.name();
                String dirName = "console/details/" + containerName.toLowerCase();
                String outName = caption + ".out.txt";
                tests.add(DynamicTest.dynamicTest(displayName, new Runner(dirName, outName, args)));
            }
        }
    }
    map.forEach((details, tests) -> nodes.add(DynamicContainer.dynamicContainer(details.name(), tests)));
    return nodes;
}
Also used : ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) DynamicTest(org.junit.jupiter.api.DynamicTest) Details(org.junit.platform.console.options.Details) Test(org.junit.jupiter.api.Test) DynamicTest(org.junit.jupiter.api.DynamicTest) Theme(org.junit.platform.console.options.Theme) DynamicNode(org.junit.jupiter.api.DynamicNode) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) EnumMap(java.util.EnumMap)

Aggregations

DynamicNode (org.junit.jupiter.api.DynamicNode)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 EnumMap (java.util.EnumMap)1 List (java.util.List)1 DynamicTest (org.junit.jupiter.api.DynamicTest)1 Test (org.junit.jupiter.api.Test)1 ExtensionContext (org.junit.jupiter.api.extension.ExtensionContext)1 JUnitException (org.junit.platform.commons.JUnitException)1 Details (org.junit.platform.console.options.Details)1 Theme (org.junit.platform.console.options.Theme)1 TestSource (org.junit.platform.engine.TestSource)1