Skip to content

Commit ba88be3

Browse files
committed
Fix: Converts try/catch methods into assertthrows methods
1 parent 0a7baed commit ba88be3

File tree

3 files changed

+14
-24
lines changed

3 files changed

+14
-24
lines changed

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/DiskdrivefullTest.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.codedifferently.lesson16.dylans_xboxTest;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.fail;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
55

66
import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException;
77
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
@@ -22,11 +22,13 @@ public void testDiskDriveFullException() throws Exception {
2222
xbox.inputGame(1);
2323
xbox.inputGame(2);
2424

25-
try {
26-
xbox.inputGame(3); // This should trigger the exception
27-
fail("Expected DiskDriveFullException to be thrown.");
28-
} catch (DiskDriveFullException e) {
29-
assertEquals("Disk drive is full. Cannot insert game.", e.getMessage());
30-
}
25+
DiskDriveFullException exception =
26+
assertThrows(
27+
DiskDriveFullException.class,
28+
() -> {
29+
xbox.inputGame(3); // This should trigger the exception
30+
});
31+
32+
assertEquals("Disk drive is full. Cannot insert game.", exception.getMessage());
3133
}
3234
}

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/LoadgameTest.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
44
import static org.junit.jupiter.api.Assertions.assertTrue;
5-
import static org.junit.jupiter.api.Assertions.fail;
65

76
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
87
import com.codedifferently.lesson16.dylans_xbox.Xbox;
@@ -11,22 +10,15 @@
1110

1211
public class LoadgameTest {
1312
@Test
14-
public void testLoadGame() {
13+
public void testLoadGame() throws Exception {
1514
// Create an instance of Xbox
1615
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
1716

1817
// Create an instance of LoadGame
1918
LoadGame loader =
2019
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
2120

22-
// Load games from the file
23-
try {
24-
loader.loadGamesFromFile(xbox);
25-
} catch (Exception e) {
26-
e.printStackTrace();
27-
fail("Exception occurred while loading games: " + e.getMessage());
28-
}
29-
21+
loader.loadGamesFromFile(xbox);
3022
// Check if the game was loaded correctly
3123
HashMap<Integer, String> games = xbox.getGames();
3224
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1)

lesson_16/objects/objects_app/src/test/java/com/codedifferently/lesson16/dylans_xboxTest/XboxTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,16 +67,12 @@ public void testXboxModelEnumValues() {
6767
}
6868

6969
@Test
70-
public void testPrintAllGames() {
70+
public void testPrintAllGames() throws Exception {
7171
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
7272
LoadGame loader =
7373
new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/data/games.csv");
74-
try {
75-
loader.loadGamesFromFile(xbox);
76-
} catch (Exception e) {
77-
e.printStackTrace();
78-
fail("Exception occurred while loading games: " + e.getMessage());
79-
}
74+
75+
loader.loadGamesFromFile(xbox); // Load games into library
8076

8177
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
8278
PrintStream originalOut = System.out;

0 commit comments

Comments
 (0)