Java Code Examples for org.testng.annotations.Test#expectedExceptions()

The following examples show how to use org.testng.annotations.Test#expectedExceptions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JUnitXMLReporter.java    From olat with Apache License 2.0 6 votes vote down vote up
private static void writeFailure(String type, Method method, Throwable ex, String msg, FileWriter out) throws IOException {
    Test annotation = method.getAnnotation(Test.class);
    if (annotation != null && ex != null) {
        Class[] expected_exceptions = annotation.expectedExceptions();
        for (int i = 0; i < expected_exceptions.length; i++) {
            Class expected_exception = expected_exceptions[i];
            if (expected_exception.equals(ex.getClass())) {
                return;
            }
        }
    }

    out.write("\n<" + type + " type=\"");
    if (ex != null) {
        out.write(ex.getClass().getName() + "\" message=\"" + escape(ex.getMessage()) + "\">");
        printException(ex, out);
    } else
        out.write("exception\" message=\"" + msg + "\">");
    out.write("\n</" + type + ">");
}
 
Example 2
Source File: JUnitXMLReporter.java    From olat with Apache License 2.0 6 votes vote down vote up
private static void writeFailure(String type, Method method, Throwable ex, String msg, FileWriter out) throws IOException {
    Test annotation = method.getAnnotation(Test.class);
    if (annotation != null && ex != null) {
        Class[] expected_exceptions = annotation.expectedExceptions();
        for (int i = 0; i < expected_exceptions.length; i++) {
            Class expected_exception = expected_exceptions[i];
            if (expected_exception.equals(ex.getClass())) {
                return;
            }
        }
    }

    out.write("\n<" + type + " type=\"");
    if (ex != null) {
        out.write(ex.getClass().getName() + "\" message=\"" + escape(ex.getMessage()) + "\">");
        printException(ex, out);
    } else
        out.write("exception\" message=\"" + msg + "\">");
    out.write("\n</" + type + ">");
}
 
Example 3
Source File: IntegrationTest.java    From djl with Apache License 2.0 5 votes vote down vote up
private static boolean expectedException(Method method, Exception e) {
    Test test = method.getAnnotation(Test.class);
    Class<?>[] exceptions = test.expectedExceptions();
    if (exceptions.length > 0) {
        Throwable exception = e.getCause();
        for (Class<?> c : exceptions) {
            if (c.isInstance(exception)) {
                return true;
            }
        }
    }
    return false;
}
 
Example 4
Source File: TckTestRunner.java    From smallrye-open-api with Apache License 2.0 5 votes vote down vote up
/**
 * @see org.junit.runners.ParentRunner#runChild(java.lang.Object, org.junit.runner.notification.RunNotifier)
 */
@Override
protected void runChild(final ProxiedTckTest child, final RunNotifier notifier) {
    OpenApiDocument.INSTANCE.set(TckTestRunner.OPEN_API_DOCS.get(child.getTest().getClass()));

    Description description = describeChild(child);
    if (isIgnored(child)) {
        notifier.fireTestIgnored(description);
    } else {
        Statement statement = new Statement() {
            @Override
            public void evaluate() throws Throwable {
                try {
                    Method testMethod = child.getTestMethod();
                    testMethod.invoke(child.getDelegate(), child.getArguments());
                } catch (InvocationTargetException e) {
                    Throwable cause = e.getCause();
                    Test testAnno = child.getTestMethod().getAnnotation(Test.class);
                    Class[] expectedExceptions = testAnno.expectedExceptions();
                    if (expectedExceptions != null && expectedExceptions.length > 0) {
                        Class expectedException = expectedExceptions[0];
                        Assert.assertEquals(expectedException, cause.getClass());
                    } else {
                        throw cause;
                    }
                }
            }
        };
        runLeaf(statement, description, notifier);
    }

}