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;
017
018import java.io.Serializable;
019
020/**
021 * Thrown if a method has not thrown an exception of the expected type.
022 * 
023 * @author rwoo
024 * @since 16.09.2011
025 */
026public class ExceptionNotThrownAssertionError 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 exception of the expected type nor
035     * another exception is thrown.
036     * 
037     * @param <E>
038     *            the type of the exception that is not thrown.
039     * @param clazz
040     *            the type of the exception that is not thrown.
041     */
042    public <E extends Exception> ExceptionNotThrownAssertionError(Class<E> clazz) {
043        super(clazz == Exception.class ? "Exception expected but not thrown"
044                : "Neither an exception of type " + clazz.getName()
045                        + " nor another exception was thrown");
046    }
047
048    /**
049     * Use this constructor if an exception of another than the expected type is
050     * thrown.
051     * 
052     * @param <E>
053     *            the type of the exception that is not thrown.
054     * @param clazz
055     *            the type of the exception that is not thrown.
056     * @param e
057     *            the exception that has been thrown instead of the expected
058     *            one.
059     */
060    public <E extends Exception> ExceptionNotThrownAssertionError(
061            Class<E> clazz, Exception e) {
062        super("Exception of type " + clazz.getName()
063                + " expected but was not thrown. "
064                + "Instead an exception of type " + e.getClass()
065                + " with message '" + e.getMessage() + "' was thrown.");
066    }
067}