001/**
002 * Copyright (C) 2011 rwoo@gmx.de
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *         http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package com.googlecode.catchexception.apis;
017
018import org.fest.assertions.api.Assertions;
019import org.fest.assertions.api.ThrowableAssert;
020
021import com.googlecode.catchexception.CatchException;
022import com.googlecode.catchexception.ExceptionNotThrownAssertionError;
023
024/**
025 * Supports <a
026 * href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD</a>-like
027 * approach to catch and verify exceptions (<i>given/when/then</i>).
028 * <p>
029 * EXAMPLE:
030 * <code><pre class="prettyprint lang-java">// given an empty list
031List myList = new ArrayList();
032
033// when we try to get the first element of the list
034when(myList).get(1);
035
036// then we expect an IndexOutOfBoundsException
037then(caughtException())
038        .isInstanceOf(IndexOutOfBoundsException.class)
039        .hasMessage("Index: 1, Size: 0") 
040        .hasNoCause();
041        
042// then we expect an IndexOutOfBoundsException (alternatively)
043thenThrown(IndexOutOfBoundsException.class);
044</pre></code>
045 * <p>
046 * The Method {@link #then(Exception)} uses <a
047 * href="https://github.com/alexruiz/fest-assert-2.x">FEST Fluent Assertions
048 * 2.x</a>. You can use them directly if you like:
049 * <code><pre class="prettyprint lang-java">// import static org.fest.assertions.Assertions.assertThat;
050
051// then we expect an IndexOutOfBoundsException
052assertThat(caughtException())
053        .isInstanceOf(IndexOutOfBoundsException.class)
054        .hasMessage("Index: 1, Size: 0") 
055        .hasMessageStartingWith("Index: 1") 
056        .hasMessageEndingWith("Size: 0") 
057        .hasMessageContaining("Size") 
058        .hasNoCause();
059</pre></code>
060 * 
061 * @author rwoo
062 * @see com.googlecode.catchexception.apis.BDDCatchException
063 * @deprecated As of release 1.3.0, replaced by {@link com.googlecode.catchexception.apis.BDDCatchException()}
064 */
065@Deprecated
066public class CatchExceptionBdd extends BDDCatchException {
067
068    /**
069     * Enables <a href="https://github.com/alexruiz/fest-assert-2.x">FEST Fluent
070     * Assertions 2.x</a> about the caught exception.
071     * <p>
072     * EXAMPLE:
073     * <code><pre class="prettyprint lang-java">// given an empty list
074List myList = new ArrayList();
075
076// when we try to get first element of the list
077when(myList).get(1);
078
079// then we expect an IndexOutOfBoundsException
080then(caughtException())
081        .isInstanceOf(IndexOutOfBoundsException.class)
082        .hasMessage("Index: 1, Size: 0") 
083        .hasMessageStartingWith("Index: 1") 
084        .hasMessageEndingWith("Size: 0") 
085        .hasMessageContaining("Size") 
086        .hasNoCause();
087</pre></code>
088     * 
089     * @param actualException
090     *            the value to be the target of the assertions methods.
091     * @return Returns the created assertion object.
092     * @see Assertions#assertThat(Throwable)
093     */
094    public static ThrowableAssert then(Exception actualException) {
095        // delegate to FEST assertions
096        return Assertions.assertThat(actualException);
097    }
098
099}