From 09404b84d0997010a05baa0fd2b80e20d61a0bbb Mon Sep 17 00:00:00 2001 From: Anthony Berg Date: Thu, 17 Oct 2024 18:34:28 +0200 Subject: [PATCH] feat(test): add invalid path test --- app/src/main/java/ReadFile.java | 7 +------ app/src/test/java/ReadFileTest.java | 8 ++++++++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/ReadFile.java b/app/src/main/java/ReadFile.java index 8e361ac..081d4e6 100644 --- a/app/src/main/java/ReadFile.java +++ b/app/src/main/java/ReadFile.java @@ -1,6 +1,4 @@ -import java.io.File; import java.io.FileNotFoundException; -import java.nio.file.InvalidPathException; import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -22,10 +20,7 @@ public class ReadFile { try { // Open the file that was defined reader = new Scanner(path.toFile()); - } catch (InvalidPathException e) { - throw new RuntimeException("Could not find the file: " + e); - } - catch (FileNotFoundException e) { + } catch (FileNotFoundException e) { throw new RuntimeException("Could not open the file: " + e); } diff --git a/app/src/test/java/ReadFileTest.java b/app/src/test/java/ReadFileTest.java index 662092f..569e602 100644 --- a/app/src/test/java/ReadFileTest.java +++ b/app/src/test/java/ReadFileTest.java @@ -52,6 +52,14 @@ class ReadFileTest { 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 @DisplayName("Reading valid file correctly") void openValidFile() throws IOException {