|
1 | 1 | package com.thealgorithms.datastructures.caches;
|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
| 4 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
4 | 5 | import static org.junit.jupiter.api.Assertions.assertNull;
|
5 | 6 |
|
6 | 7 | import org.junit.jupiter.api.Test;
|
@@ -127,19 +128,30 @@ public void testEmptyCacheBehavior() {
|
127 | 128 | public void capacityValidation() {
|
128 | 129 | // Check that an exception is thrown for invalid capacities
|
129 | 130 | Exception exception = null;
|
| 131 | + |
| 132 | + // Testing for zero capacity |
130 | 133 | try {
|
131 | 134 | new MRUCache<>(0); // Should throw an exception
|
132 | 135 | } catch (IllegalArgumentException e) {
|
133 |
| - exception = e; |
| 136 | + exception = e; // Store the exception if caught |
134 | 137 | }
|
| 138 | + |
| 139 | + // Ensure exception is not null before accessing its message |
| 140 | + assertNotNull(exception, "Expected IllegalArgumentException for capacity 0"); |
135 | 141 | assertEquals("Capacity must be greater than 0!", exception.getMessage());
|
136 | 142 |
|
| 143 | + // Resetting exception for the next test |
137 | 144 | exception = null;
|
| 145 | + |
| 146 | + // Testing for negative capacity |
138 | 147 | try {
|
139 | 148 | new MRUCache<>(-1); // Should throw an exception
|
140 | 149 | } catch (IllegalArgumentException e) {
|
141 |
| - exception = e; |
| 150 | + exception = e; // Store the exception if caught |
142 | 151 | }
|
| 152 | + |
| 153 | + // Ensure exception is not null before accessing its message |
| 154 | + assertNotNull(exception, "Expected IllegalArgumentException for capacity -1"); |
143 | 155 | assertEquals("Capacity must be greater than 0!", exception.getMessage());
|
144 | 156 | }
|
145 | 157 | }
|
0 commit comments