1 /**
2 * Powerunit - A JDK1.8 test framework
3 * Copyright (C) 2014 Mathieu Boretti.
4 *
5 * This file is part of Powerunit
6 *
7 * Powerunit is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * Powerunit is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
19 */
20 package ch.powerunit;
21
22 import org.hamcrest.Matcher;
23
24 /**
25 * DSL for assertion on exception result.
26 * <p>
27 * This interface is returned by the various methods
28 * {@link TestSuite#assertWhen(Statement) assertWhen} exposed by
29 * {@link TestSuite}.
30 *
31 * @author borettim
32 *
33 * @param <T>
34 * The exception type
35 */
36 public interface AssertThatException<T extends Throwable> {
37
38 /**
39 * Define the matcher on the exception and execute the matcher validation.
40 * <p>
41 * <br>
42 * <i>By default, assertion can only be used from the main thread of the
43 * test ; When used from another thread, the assertion will be lost. In the
44 * case the {@link Test#fastFail() fastFail} attribute of {@link Test @Test}
45 * annotation is used, the assertion may not be lost, in case the thread use
46 * an assertion method from the test object instance. </i>
47 * <p>
48 * For instance, having this method :
49 *
50 * <pre>
51 * public static void method1() throws Exception {
52 * throw new Exception("demo1");
53 * }
54 * </pre>
55 *
56 * It is possible to test the message by using :
57 *
58 * <pre>
59 * assertWhen(asStatement(DemoAssertThatExceptionTest::method1)).throwException(
60 * exceptionMessage("demo1"));
61 * </pre>
62 *
63 * @param matching
64 * the matcher.
65 * @return true if the assertion is valid ; If the assertion is false,
66 * depending on {@link Test#fastFail()} : If <code>true</code>, then
67 * fail the test, else, return false and the test will be failed
68 * later.
69 * @see Test#fastFail() The documentation of the <code>fastFail</code>
70 * attribute of the <code>@Test</code> annotation, regarding the action
71 * done by this assertion.
72 */
73 boolean throwException(Matcher<? super T> matching);
74 }