View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.lang3;
19  
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.function.Executable;
24  
25  /**
26   * Specialized APIs to complement {@link org.junit.jupiter.api.Assertions}.
27   */
28  public class LangAssertions {
29  
30      /**
31       * Asserts that execution of the given {@code executable} throws a {@link IllegalArgumentException}.
32       *
33       * <p>
34       * The assertion passes if the thrown exception type is the same as {@link IllegalArgumentException} or a subtype. To check for the exact thrown type use
35       * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is
36       * thrown, this method fails.
37       * </p>
38       *
39       * @param executable What to test.
40       * @return The thrown IllegalArgumentException.
41       * @see Assertions#assertThrowsExactly(Class, Executable)
42       */
43      public static IllegalArgumentException assertIllegalArgumentException(final Executable executable) {
44          return assertThrows(IllegalArgumentException.class, executable);
45      }
46  
47      /**
48       * Asserts that execution of the given {@code executable} throws a {@link IllegalArgumentException}.
49       *
50       * <p>
51       * The assertion passes if the thrown exception type is the same as {@link IllegalArgumentException} or a subtype. To check for the exact thrown type use
52       * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is
53       * thrown, this method fails with the given {@code message}.
54       * </p>
55       *
56       * @param executable What to test.
57       * @param message    The message for the failure if the executable doesn't throw a IllegalArgumentException.
58       * @return The thrown IllegalArgumentException.
59       * @see Assertions#assertThrowsExactly(Class, Executable)
60       */
61      public static IllegalArgumentException assertIllegalArgumentException(final Executable executable, final String message) {
62          return assertThrows(IllegalArgumentException.class, executable, message);
63      }
64  
65      /**
66       * Asserts that execution of the given {@code executable} throws a {@link IndexOutOfBoundsException}.
67       *
68       * <p>
69       * The assertion passes if the thrown exception type is the same as {@link IndexOutOfBoundsException} or a subtype. To check for the exact thrown type use
70       * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is
71       * thrown, this method fails.
72       * </p>
73       *
74       * @param executable What to test.
75       * @return The thrown IndexOutOfBoundsException.
76       * @see Assertions#assertThrowsExactly(Class, Executable)
77       */
78      public static IndexOutOfBoundsException assertIndexOutOfBoundsException(final Executable executable) {
79          return assertThrows(IndexOutOfBoundsException.class, executable);
80      }
81  
82      /**
83       * Asserts that execution of the given {@code executable} throws a {@link IndexOutOfBoundsException}.
84       *
85       * <p>
86       * The assertion passes if the thrown exception type is the same as {@link IndexOutOfBoundsException} or a subtype. To check for the exact thrown type use
87       * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is
88       * thrown, this method fails with the given {@code message}.
89       * </p>
90       *
91       * @param executable What to test.
92       * @param message    The message for the failure if the executable doesn't throw a IndexOutOfBoundsException.
93       * @return The thrown IndexOutOfBoundsException.
94       * @see Assertions#assertThrowsExactly(Class, Executable)
95       */
96      public static IndexOutOfBoundsException assertIndexOutOfBoundsException(final Executable executable, final String message) {
97          return assertThrows(IndexOutOfBoundsException.class, executable, message);
98      }
99  
100     /**
101      * Asserts that execution of the given {@code executable} throws a {@link NullPointerException}.
102      *
103      * <p>
104      * The assertion passes if the thrown exception type is the same as {@link NullPointerException} or a subtype. To check for the exact thrown type use
105      * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is
106      * thrown, this method fails.
107      * </p>
108      *
109      * @param executable What to test.
110      * @return The thrown NullPointerException.
111      * @see Assertions#assertThrowsExactly(Class, Executable)
112      */
113     public static NullPointerException assertNullPointerException(final Executable executable) {
114         return assertThrows(NullPointerException.class, executable);
115     }
116 
117     /**
118      * Asserts that execution of the given {@code executable} throws a {@link NullPointerException}.
119      *
120      * <p>
121      * The assertion passes if the thrown exception type is the same as {@link NullPointerException} or a subtype. To check for the exact thrown type use
122      * {@link Assertions#assertThrowsExactly(Class, Executable) assertThrowsExactly}. If no exception is thrown, or if an exception of a different type is
123      * thrown, this method fails with the given {@code message}.
124      * </p>
125      *
126      * @param executable What to test.
127      * @param message    The message for the failure if the executable doesn't throw a NullPointerException.
128      * @return The thrown NullPointerException.
129      * @see Assertions#assertThrowsExactly(Class, Executable)
130      */
131     public static NullPointerException assertNullPointerException(final Executable executable, final String message) {
132         return assertThrows(NullPointerException.class, executable, message);
133     }
134 }