feat(test): add invalid path test

This commit is contained in:
Anthony Berg 2024-10-17 18:34:28 +02:00
parent c60ac6227c
commit 09404b84d0
2 changed files with 9 additions and 6 deletions

View File

@ -1,6 +1,4 @@
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.nio.file.InvalidPathException;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -22,10 +20,7 @@ public class ReadFile {
try { try {
// Open the file that was defined // Open the file that was defined
reader = new Scanner(path.toFile()); reader = new Scanner(path.toFile());
} catch (InvalidPathException e) { } catch (FileNotFoundException e) {
throw new RuntimeException("Could not find the file: " + e);
}
catch (FileNotFoundException e) {
throw new RuntimeException("Could not open the file: " + e); throw new RuntimeException("Could not open the file: " + e);
} }

View File

@ -52,6 +52,14 @@ class ReadFileTest {
assertThrowsExactly(RuntimeException.class, () -> ReadFile.open(nonExistent)); assertThrowsExactly(RuntimeException.class, () -> ReadFile.open(nonExistent));
} }
@Test
@DisplayName("Testing for an invalid path")
void invalidPath() {
Path invalid = Path.of("//invalid.txt");
assertThrowsExactly(RuntimeException.class, () -> ReadFile.open(invalid));
}
@Test @Test
@DisplayName("Reading valid file correctly") @DisplayName("Reading valid file correctly")
void openValidFile() throws IOException { void openValidFile() throws IOException {