Java Assert assertDidNotThrow(final Runnable r)

Here you can find the source of assertDidNotThrow(final Runnable r)

Description

If Java assertions are enabled, execute the given Runnable , and assert(false) if it throws any Throwable .
Can be used for asserting upon stuff which does not return false upon error but throws: A regular Java assert could only consume boolean-returning functions.

The Throwable will be passed as message to the assert, it will not be thrown.

License

Open Source License

Declaration

public static final void assertDidNotThrow(final Runnable r) 

Method Source Code

//package com.java2s;
/* This code is part of WoT, a plugin for Freenet. It is distributed 
 * under the GNU General Public License, version 2 (or at your option
 * any later version). See http://www.gnu.org/ for details of the GPL. */

public class Main {
    /**/*from www  . j a v a  2 s .c o m*/
     * If Java assertions are enabled, execute the given {@link Runnable}, and assert(false) if it
     * throws any {@link Throwable}.<br>
     * Can be used for asserting upon stuff which does not return false upon error but throws:
     * A regular Java assert could only consume boolean-returning functions.<br><br>
     * 
     * The {@link Throwable} will be passed as message to the assert, it will not be thrown.<br>
     */
    public static final void assertDidNotThrow(final Runnable r) {
        boolean execute = false;

        assert (execute = true);

        if (execute) {
            try {
                r.run();
            } catch (Throwable t) {
                assert (false) : t;
            }
        }
    }
}

Related

  1. assertCondition(boolean condition, String message)
  2. assertCondition(boolean condition, String msg)
  3. assertContains(final String string, final String content)
  4. assertContains(String needle, String haystack)
  5. assertDescriptorName(String key)
  6. assertDifferentInstance(final Object o1, final Object o2)
  7. assertDotted(String name)
  8. assertEntryCount(final long entryCount)
  9. assertEnvVarPresent(String envVarKey, String errorMessage)