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