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.throwable.apis; 017 018import org.fest.assertions.api.Assertions; 019import org.fest.assertions.api.ThrowableAssert; 020 021import com.googlecode.catchexception.throwable.CatchThrowable; 022import com.googlecode.catchexception.throwable.ThrowableNotThrownAssertionError; 023 024/** 025 * Supports <a href="http://en.wikipedia.org/wiki/Behavior_Driven_Development">BDD</a>-like approach to catch and verify 026 * throwables (<i>given/when/then</i>). 027 * <p> 028 * EXAMPLE: <code><pre class="prettyprint lang-java">// given an empty list 029 List myList = new ArrayList(); 030 031 // when we try to get the first element of the list 032 when(myList).get(1); 033 034 // then we expect an IndexOutOfBoundsThrowable 035 then(caughtThrowable()) 036 .isInstanceOf(IndexOutOfBoundsThrowable.class) 037 .hasMessage("Index: 1, Size: 0") 038 .hasNoCause(); 039 040 // then we expect an IndexOutOfBoundsThrowable (alternatively) 041 thenThrown(IndexOutOfBoundsThrowable.class); 042 </pre></code> 043 * <p> 044 * The Method {@link #then(Throwable)} uses <a href="https://github.com/alexruiz/fest-assert-2.x">FEST Fluent Assertions 045 * 2.x</a>. You can use them directly if you like: 046 * <code><pre class="prettyprint lang-java">// import static org.fest.assertions.Assertions.assertThat; 047 048 // then we expect an IndexOutOfBoundsThrowable 049 assertThat(caughtThrowable()) 050 .isInstanceOf(IndexOutOfBoundsThrowable.class) 051 .hasMessage("Index: 1, Size: 0") 052 .hasMessageStartingWith("Index: 1") 053 .hasMessageEndingWith("Size: 0") 054 .hasMessageContaining("Size") 055 .hasNoCause(); 056 </pre></code> 057 * 058 * @author rwoo 059 * @since 1.2.0 060 * @see com.googlecode.catchexception.throwable.apis.BDDCatchThrowable 061 * @deprecated As of release 1.3.0, replaced by {@link com.googlecode.catchexception.throwable.apis.BDDCatchThrowable()} 062 * 063 */ 064@Deprecated 065public class CatchThrowableBdd extends BDDCatchThrowable { 066 067 /** 068 * Enables <a href="https://github.com/alexruiz/fest-assert-2.x">FEST Fluent Assertions 2.x</a> about the caught 069 * throwable. 070 * <p> 071 * EXAMPLE: <code><pre class="prettyprint lang-java">// given an empty list 072 List myList = new ArrayList(); 073 074 // when we try to get first element of the list 075 when(myList).get(1); 076 077 // then we expect an IndexOutOfBoundsThrowable 078 then(caughtThrowable()) 079 .isInstanceOf(IndexOutOfBoundsThrowable.class) 080 .hasMessage("Index: 1, Size: 0") 081 .hasMessageStartingWith("Index: 1") 082 .hasMessageEndingWith("Size: 0") 083 .hasMessageContaining("Size") 084 .hasNoCause(); 085 </pre></code> 086 * 087 * @param actualThrowable 088 * the value to be the target of the assertions methods. 089 * @return Returns the created assertion object. 090 * @see Assertions#assertThat(Throwable) 091 */ 092 public static ThrowableAssert then(Throwable actualThrowable) { 093 // delegate to FEST assertions 094 return Assertions.assertThat(actualThrowable); 095 } 096 097}