Java Assert assertThrows(Class exception, Runnable action)

Here you can find the source of assertThrows(Class exception, Runnable action)

Description

assert Throws

License

Apache License

Declaration

public static void assertThrows(Class<? extends Exception> exception,
            Runnable action) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    public static void assertThrows(Class<? extends Exception> exception,
            Runnable action) {/*from   w  w w.ja v  a2s.  c om*/
        try {
            action.run();
            throw new AssertionError("Expected exception: "
                    + exception.getTypeName() + ", but no exception thrown");
        } catch (Exception e) {
            if (!e.getClass().equals(exception)) {
                throw new AssertionError("Expected exception: "
                        + exception.getTypeName() + ", but was "
                        + e.getClass().getTypeName());
            }
        }
    }
}

Related

  1. assertStringMatch(String first, String second)
  2. assertStringNotEmpty(String str, String name)
  3. assertStringNotEmpty(String str, String name)
  4. assertStringNotEmpty(String string, String fieldName)
  5. assertTesting()
  6. assertTwoParameters(Class clz, String method, double... args)
  7. assertUnreachable()
  8. assertValid(int piece)
  9. assertValidBrowserType(int aBrowserType)