|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
4 | 4 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
5 | 5 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 6 | +import static org.junit.jupiter.api.Assertions.fail; |
6 | 7 |
|
7 | 8 | import org.junit.jupiter.api.Test; |
8 | 9 |
|
@@ -132,32 +133,28 @@ public void capacityValidation() { |
132 | 133 | // Testing for zero capacity |
133 | 134 | try { |
134 | 135 | new MRUCache<>(0); // Should throw an exception |
| 136 | + // If no exception is thrown, we should fail the test |
| 137 | + fail("Expected IllegalArgumentException for capacity 0"); |
135 | 138 | } catch (IllegalArgumentException e) { |
136 | 139 | exception = e; // Store the exception if caught |
137 | 140 | } |
138 | 141 |
|
139 | | - // Ensure exception is not null before accessing its message |
| 142 | + // Ensure exception is not null and check the message |
140 | 143 | assertNotNull(exception, "Expected IllegalArgumentException for capacity 0"); |
141 | | - |
142 | | - if (exception != null) { |
143 | | - assertEquals("Capacity must be greater than 0!", exception.getMessage()); |
144 | | - } |
| 144 | + assertEquals("Capacity must be greater than 0!", exception.getMessage()); |
145 | 145 |
|
146 | 146 | // Resetting exception for the next test |
147 | | - exception = null; |
148 | | - |
149 | 147 | // Testing for negative capacity |
150 | 148 | try { |
151 | 149 | new MRUCache<>(-1); // Should throw an exception |
| 150 | + // If no exception is thrown, we should fail the test |
| 151 | + fail("Expected IllegalArgumentException for capacity -1"); |
152 | 152 | } catch (IllegalArgumentException e) { |
153 | 153 | exception = e; // Store the exception if caught |
154 | 154 | } |
155 | 155 |
|
156 | | - // Ensure exception is not null before accessing its message |
| 156 | + // Ensure exception is not null and check the message |
157 | 157 | assertNotNull(exception, "Expected IllegalArgumentException for capacity -1"); |
158 | | - |
159 | | - if (exception != null) { |
160 | | - assertEquals("Capacity must be greater than 0!", exception.getMessage()); |
161 | | - } |
| 158 | + assertEquals("Capacity must be greater than 0!", exception.getMessage()); |
162 | 159 | } |
163 | 160 | } |
0 commit comments