Skip to content

Commit 40b7f7d

Browse files
committed
refactor: Enhance docs, add more tests in DynamicArray
1 parent 7c40f77 commit 40b7f7d

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/test/java/com/thealgorithms/datastructures/caches/MRUCacheTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.datastructures.caches;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNotNull;
45
import static org.junit.jupiter.api.Assertions.assertNull;
56

67
import org.junit.jupiter.api.Test;
@@ -127,19 +128,30 @@ public void testEmptyCacheBehavior() {
127128
public void capacityValidation() {
128129
// Check that an exception is thrown for invalid capacities
129130
Exception exception = null;
131+
132+
// Testing for zero capacity
130133
try {
131134
new MRUCache<>(0); // Should throw an exception
132135
} catch (IllegalArgumentException e) {
133-
exception = e;
136+
exception = e; // Store the exception if caught
134137
}
138+
139+
// Ensure exception is not null before accessing its message
140+
assertNotNull(exception, "Expected IllegalArgumentException for capacity 0");
135141
assertEquals("Capacity must be greater than 0!", exception.getMessage());
136142

143+
// Resetting exception for the next test
137144
exception = null;
145+
146+
// Testing for negative capacity
138147
try {
139148
new MRUCache<>(-1); // Should throw an exception
140149
} catch (IllegalArgumentException e) {
141-
exception = e;
150+
exception = e; // Store the exception if caught
142151
}
152+
153+
// Ensure exception is not null before accessing its message
154+
assertNotNull(exception, "Expected IllegalArgumentException for capacity -1");
143155
assertEquals("Capacity must be greater than 0!", exception.getMessage());
144156
}
145157
}

0 commit comments

Comments
 (0)