Last active
December 8, 2021 20:14
-
-
Save MWhyte/8c4dd1dbcf3a556d9cd777503bf5c521 to your computer and use it in GitHub Desktop.
Post: Exception Handling in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A Post: Exception Handling in Java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.mwhyte.errors; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class OOMExample { | |
private final List<Integer> scores = new ArrayList<>(); | |
public void addScore(Integer score) { | |
scores.add(score); | |
} | |
public List<Integer> getScores() { | |
return scores; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package dev.mwhyte.errors; | |
import static org.junit.Assert.assertThrows; | |
import static org.junit.jupiter.api.Assertions.*; | |
import org.junit.jupiter.api.BeforeEach; | |
import org.junit.jupiter.api.Test; | |
class OOMExampleTest { | |
private OOMExample oomExample; | |
@BeforeEach | |
void setUp() { | |
oomExample = new OOMExample(); | |
} | |
@Test | |
void addScore_smallNumbers() { | |
for (int i = 0; i < 100; i++) { | |
oomExample.addScore(i); | |
} | |
assertEquals(100, oomExample.getScores().size()); | |
} | |
@Test | |
void addScore_largeNumbers() { | |
assertThrows(OutOfMemoryError.class, () -> { | |
for (int i = 0; i < Integer.MAX_VALUE; i++) { | |
oomExample.addScore(i); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment