001/**
002 * Powerunit - A JDK1.8 test framework
003 * Copyright (C) 2014 Mathieu Boretti.
004 *
005 * This file is part of Powerunit
006 *
007 * Powerunit is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation, either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * Powerunit is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
019 */
020package ch.powerunit;
021
022import org.hamcrest.Matcher;
023
024/**
025 * DSL for assertion on exception result.
026 * <p>
027 * This interface is returned by the various methods
028 * {@link TestSuite#assertWhen(Statement) assertWhen} exposed by
029 * {@link TestSuite}.
030 *
031 * @author borettim
032 *
033 * @param <T>
034 *            The exception type
035 */
036public interface AssertThatException<T extends Throwable> {
037
038        /**
039         * Define the matcher on the exception and execute the matcher validation.
040         * <p>
041         * <br>
042         * <i>By default, assertion can only be used from the main thread of the
043         * test ; When used from another thread, the assertion will be lost. In the
044         * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
045         * annotation is used, the assertion may not be lost, in case the thread use
046         * an assertion method from the test object instance. </i>
047         * <p>
048         * For instance, having this method :
049         * 
050         * <pre>
051         * public static void method1() throws Exception {
052         *      throw new Exception(&quot;demo1&quot;);
053         * }
054         * </pre>
055         * 
056         * It is possible to test the message by using :
057         * 
058         * <pre>
059         * assertWhen(asStatement(DemoAssertThatExceptionTest::method1)).throwException(
060         *              exceptionMessage(&quot;demo1&quot;));
061         * </pre>
062         * 
063         * @param matching
064         *            the matcher.
065         * @return true if the assertion is valid ; If the assertion is false,
066         *         depending on {@link Test#fastFail()} : If <code>true</code>, then
067         *         fail the test, else, return false and the test will be failed
068         *         later.
069         * @see Test#fastFail() The documentation of the <code>fastFail</code>
070         *      attribute of the <code>@Test</code> annotation, regarding the action
071         *      done by this assertion.
072         */
073        boolean throwException(Matcher<? super T> matching);
074}