Skip to content

Commit 4549442

Browse files
committed
test: add unit tests for DiskDriveFullException and LoadGame functionality, Also simplifies imports for XboxTest
1 parent f52c9c8 commit 4549442

File tree

3 files changed

+71
-5
lines changed

3 files changed

+71
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package XboxTest;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.fail;
5+
import org.junit.jupiter.api.Test;
6+
7+
import com.codedifferently.lesson16.dylans_xbox.DiskDriveFullException;
8+
import com.codedifferently.lesson16.dylans_xbox.Xbox;
9+
10+
public class DiskdrivefullTest {
11+
@Test
12+
public void testDiskDriveFullException() throws Exception {
13+
// Create an instance of Xbox with diskDriveFull set to true
14+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, true);
15+
16+
try {
17+
xbox.inputGame(1, "Call of Duty");
18+
fail("Expected DiskDriveFullException to be thrown.");
19+
} catch (DiskDriveFullException e) {
20+
assertEquals("Disk drive is full. Cannot insert game.", e.getMessage());
21+
}
22+
}
23+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package XboxTest;
2+
3+
import java.util.HashMap;
4+
5+
import static org.junit.jupiter.api.Assertions.assertEquals;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
7+
import static org.junit.jupiter.api.Assertions.fail;
8+
import org.junit.jupiter.api.Test;
9+
10+
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
11+
import com.codedifferently.lesson16.dylans_xbox.Xbox;
12+
13+
public class LoadgameTest {
14+
@Test
15+
public void testLoadGame() {
16+
// Create an instance of Xbox
17+
Xbox xbox = new Xbox("XBOXSERIESX", 600, "Black", true, false);
18+
19+
// Create an instance of LoadGame
20+
LoadGame loader = new LoadGame("src/main/java/com/codedifferently/lesson16/dylans_xbox/games.csv");
21+
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+
30+
// Check if the game was loaded correctly
31+
HashMap<Integer, String> games = xbox.getGames();
32+
assertTrue(games.containsKey(1)); // Check that the first game is loaded (ID 1)
33+
assertEquals("Call of Duty", games.get(1)); // Ensure the first game matches the CSV
34+
}
35+
}

lesson_16/objects/objects_app/src/test/java/XboxTest/XboxTest.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package XboxTest;
22

3-
import static org.junit.jupiter.api.Assertions.assertEquals; // Ensure LoadGame is imported
3+
import java.io.ByteArrayOutputStream; // Ensure LoadGame is imported
4+
import java.io.PrintStream;
5+
import java.util.HashMap;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
48
import static org.junit.jupiter.api.Assertions.assertTrue;
59
import static org.junit.jupiter.api.Assertions.fail;
10+
import org.junit.jupiter.api.Test;
611

712
import com.codedifferently.lesson16.dylans_xbox.LoadGame;
813
import com.codedifferently.lesson16.dylans_xbox.Xbox;
9-
import java.io.ByteArrayOutputStream;
10-
import java.io.PrintStream;
11-
import java.util.HashMap;
12-
import org.junit.jupiter.api.Test;
1314

1415
public class XboxTest {
1516

@@ -137,4 +138,11 @@ public void testGetGames() {
137138

138139
assertEquals(10, games.size(), "There should be 10 games loaded.");
139140
}
141+
142+
@Test
143+
public void testGetPrice() {
144+
Xbox xbox = new Xbox("XBOX360", 400, "White", true, false);
145+
int price = xbox.getPrice();
146+
assertEquals(400, price, "The price should be 400.");
147+
}
140148
}

0 commit comments

Comments
 (0)