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