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.apis;
17  
18  import static com.googlecode.catchexception.throwable.apis.BDDCatchThrowable.caughtThrowable;
19  import static com.googlecode.catchexception.throwable.apis.BDDCatchThrowable.when;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.List;
25  
26  import org.assertj.core.api.BDDAssertions;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Tests {@link com.googlecode.catchexception.throwable.apis.BDDCatchThrowable}.
31   */
32  class BDDCatchThrowableTest {
33  
34      /**
35       * The message of the exception thrown by {@code new ArrayList<String>().get(0) } for jdk9on.
36       */
37      private final String expectedMessageJdk9on = "Index 1 out of bounds for length 0";
38  
39      /**
40       * The message of the exception thrown for jdk9on for 500.
41       */
42      private final String expectedMessageJdk9on500 = "Index 500 out of bounds for length 9";
43  
44      /**
45       * Then.
46       */
47      @SuppressWarnings("rawtypes")
48      @Test
49      void then() {
50          // given an empty list
51          List myList = new ArrayList();
52  
53          // when we try to get first element of the list
54          when(() -> myList.get(1));
55  
56          // then we expect an IndexOutOfBoundsException
57          if (!caughtThrowable().getMessage().contains(expectedMessageJdk9on)) {
58              BDDAssertions.then(caughtThrowable()) //
59                      .isInstanceOf(IndexOutOfBoundsException.class) //
60                      .hasMessage("Index: 1, Size: 0") //
61                      .hasNoCause();
62          }
63  
64      }
65  
66      /**
67       * Assert J then.
68       */
69      @SuppressWarnings("rawtypes")
70      @Test
71      void assertJThen() {
72          // given an empty list
73          List myList = new ArrayList();
74  
75          // when we try to get first element of the list
76          when(() -> myList.get(1));
77  
78          // then we expect an IndexOutOfBoundsException
79          if (!caughtThrowable().getMessage().contains(expectedMessageJdk9on)) {
80              BDDAssertions.then(caughtThrowable()).isInstanceOf(IndexOutOfBoundsException.class) //
81                      .hasMessage("Index: 1, Size: 0") //
82                      .hasNoCause();
83          }
84  
85      }
86  
87      /**
88       * Then thrown.
89       */
90      @SuppressWarnings("rawtypes")
91      @Test
92      void thenThrown() {
93  
94          // given a list with nine elements
95          List myList = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9);
96  
97          // when we try to get the 500th element
98          when(() -> myList.get(500));
99  
100         // then we expect an IndexOutOfBoundsException
101         BDDCatchThrowable.thenThrown(IndexOutOfBoundsException.class);
102 
103         // test: caughtThrowable() ==null
104         when(() -> myList.get(0));
105         try {
106             BDDCatchThrowable.thenThrown(IndexOutOfBoundsException.class);
107 
108         } catch (AssertionError e) {
109             assertEquals("Neither a throwable of type java.lang." + "IndexOutOfBoundsException nor another throwable "
110                     + "was thrown", e.getMessage());
111         }
112 
113         // test: caughtThrowable() is not IllegalArgumentException
114         when(() -> myList.get(500));
115         try {
116             BDDCatchThrowable.thenThrown(IllegalArgumentException.class);
117 
118         } catch (AssertionError e) {
119             if (!e.getMessage().contains(expectedMessageJdk9on500)) {
120                 assertEquals("Throwable of type java.lang.IllegalArgumentException"
121                         + " expected but was not thrown. Instead a throwable of"
122                         + " type class java.lang.ArrayIndexOutOfBoundsException" + " with message '500' was "
123                         + "thrown.", e.getMessage());
124             }
125         }
126 
127     }
128 
129 }