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     */
016    package com.googlecode.catchexception.throwable;
017    
018    import java.io.Serializable;
019    
020    /**
021     * Thrown if a method has not thrown an throwable of the expected type.
022     * 
023     * @author rwoo
024     * @since 16.09.2011
025     */
026    public class ThrowableNotThrownAssertionError extends AssertionError {
027    
028        /**
029         * See {@link Serializable}.
030         */
031        private static final long serialVersionUID = 7044423604241785057L;
032    
033        /**
034         * Use this constructor if neither an throwable of the expected type nor another throwable is thrown.
035         * 
036         * @param <E>
037         *            the type of the throwable that is not thrown.
038         * @param clazz
039         *            the type of the throwable that is not thrown.
040         */
041        public <E extends Throwable> ThrowableNotThrownAssertionError(Class<E> clazz) {
042            super(clazz == Throwable.class ? "Throwable expected but not thrown" : "Neither a throwable of type "
043                    + clazz.getName() + " nor another throwable was thrown");
044        }
045    
046        /**
047         * Use this constructor if an throwable of another than the expected type is thrown.
048         * 
049         * @param <E>
050         *            the type of the throwable that is not thrown.
051         * @param clazz
052         *            the type of the throwable that is not thrown.
053         * @param e
054         *            the throwable that has been thrown instead of the expected one.
055         */
056        public <E extends Throwable> ThrowableNotThrownAssertionError(Class<E> clazz, Throwable e) {
057            super("Throwable of type " + clazz.getName() + " expected but was not thrown. "
058                    + "Instead a throwable of type " + e.getClass() + " with message '" + e.getMessage() + "' was thrown.");
059        }
060    }