diff --git a/src/main/java/com/serenitydojo/exceptions/FileLoader.java b/src/main/java/com/serenitydojo/exceptions/FileLoader.java index 98f7b84..74907c9 100644 --- a/src/main/java/com/serenitydojo/exceptions/FileLoader.java +++ b/src/main/java/com/serenitydojo/exceptions/FileLoader.java @@ -1,19 +1,29 @@ package com.serenitydojo.exceptions; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; public class FileLoader { public String readHelloWorld() throws IOException { - return "";//Files.readString(Paths.get("src/main/resources/hello.txt")); + return Files.readString(Paths.get("src/main/resources/hello.txt")); } - public Boolean fileContainsText(String filename, String expectedText) { + public Boolean fileContainsText(String filename, String expectedText) throws IOException { String path = "src/main/resources/" + filename; - return null;// (Files.readString(Paths.get(path)).contains(expectedText)); + try { + return (Files.readString(Paths.get(path)).contains(expectedText)); + } catch (IOException e){ + return false; + } } - public Boolean fileHasText(String filename, String expectedText) { + public Boolean fileHasText(String filename, String expectedText) throws IOException { String path = "src/main/resources/" + filename; - return null;// (Files.readString(Paths.get(path)).contains(expectedText)); + try { + return (Files.readString(Paths.get(path)).contains(expectedText)); + } catch (IOException e){ + throw new MissingWelcomeFileException("Missing welcome file: " + filename, e); + } } } diff --git a/src/test/java/com/serenitydojo/exceptions/ExceptionHandlingExercises.java b/src/test/java/com/serenitydojo/exceptions/ExceptionHandlingExercises.java index 8724a1f..655908f 100644 --- a/src/test/java/com/serenitydojo/exceptions/ExceptionHandlingExercises.java +++ b/src/test/java/com/serenitydojo/exceptions/ExceptionHandlingExercises.java @@ -31,13 +31,13 @@ public void workingWithDeclaredExceptions() throws IOException { * does not contain the excepted text, or if the file does not exist. */ @Test - public void catchingExceptions() { + public void catchingExceptions() throws IOException { FileLoader fileLoader = new FileLoader(); assertThat(fileLoader.fileContainsText("hello.txt","Hello World")).isTrue(); } @Test - public void catchingExceptionsWhenTheFileDoesNotExist() { + public void catchingExceptionsWhenTheFileDoesNotExist() throws IOException { FileLoader fileLoader = new FileLoader(); assertThat(fileLoader.fileContainsText("does-not-exist.txt","Hello World")).isFalse(); } @@ -48,7 +48,7 @@ public void catchingExceptionsWhenTheFileDoesNotExist() { * and update the fileHasText() method to throw this exception if no matching file is found. */ @Test(expected = MissingWelcomeFileException.class) - public void catchingCustomExceptionsWhenTheFileDoesNotExist() { + public void catchingCustomExceptionsWhenTheFileDoesNotExist() throws IOException { FileLoader fileLoader = new FileLoader(); assertThat(fileLoader.fileHasText("does-not-exist.txt","Hello World")).isFalse(); }