View Javadoc
1   /*
2    * Copyright 2011-2024 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.googlecode.catchexception.throwable;
17  
18  import java.io.Serializable;
19  
20  /**
21   * Thrown if a method has not thrown an throwable of the expected type.
22   */
23  public class ThrowableNotThrownAssertionError extends AssertionError {
24  
25      /**
26       * See {@link Serializable}.
27       */
28      private static final long serialVersionUID = 7044423604241785057L;
29  
30      /**
31       * Use this constructor if neither an throwable of the expected type nor another throwable is thrown.
32       *
33       * @param <E>
34       *            the type of the throwable that is not thrown.
35       * @param clazz
36       *            the type of the throwable that is not thrown.
37       */
38      public <E extends Throwable> ThrowableNotThrownAssertionError(Class<E> clazz) {
39          super(clazz == Throwable.class ? "Throwable expected but not thrown"
40                  : "Neither a throwable of type " + clazz.getName() + " nor another throwable was thrown");
41      }
42  
43      /**
44       * Use this constructor if an throwable of another than the expected type is thrown.
45       *
46       * @param <E>
47       *            the type of the throwable that is not thrown.
48       * @param clazz
49       *            the type of the throwable that is not thrown.
50       * @param e
51       *            the throwable that has been thrown instead of the expected one.
52       */
53      public <E extends Throwable> ThrowableNotThrownAssertionError(Class<E> clazz, Throwable e) {
54          super("Throwable of type " + clazz.getName() + " expected but was not thrown. " + "Instead a throwable of type "
55                  + e.getClass() + " with message '" + e.getMessage() + "' was thrown.");
56      }
57  
58  }