Skip to content

Commit

Permalink
Add test for expected exception
Browse files Browse the repository at this point in the history
  • Loading branch information
bmuschko committed Dec 21, 2017
1 parent bd44ea9 commit d2bc1f7
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
Expand Up @@ -8,6 +8,10 @@ public class DefaultFileReader implements FileReader {

@Override
public String readContent(Path path) throws IOException {
if (Files.notExists(path)) {
throw new IOException("File does not exist");
}

return new String(Files.readAllBytes(path));
}
}
@@ -0,0 +1,19 @@
package com.bmuschko.test.comparison.spock

import com.bmuschko.test.comparison.DefaultFileReader
import spock.lang.FailsWith
import spock.lang.Specification
import spock.lang.Subject

import java.nio.file.Paths

class ExpectedExceptionTest extends Specification {

@Subject def fileReader = new DefaultFileReader()

@FailsWith(IOException)
def "cannot read non-existent file"() {
expect:
fileReader.readContent(Paths.get('hello.text'))
}
}
@@ -0,0 +1,21 @@
package com.bmuschko.test.comparison.junit5;

import com.bmuschko.test.comparison.DefaultFileReader;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.file.Paths;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class ExpectedExceptionTest {

private final DefaultFileReader fileReader = new DefaultFileReader();

@Test
void cannotReadNonExistentFile() {
assertThrows(IOException.class, () -> {
fileReader.readContent(Paths.get("hello.text"));
});
}
}

0 comments on commit d2bc1f7

Please sign in to comment.