Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/main/java/com/serenitydojo/exceptions/FileLoader.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand Down