Exceptions and Junit
Figure 513. Expected exceptions in Junit
@Test(expected = FileAlreadyExistsException.class)
public void copyFile() throws IOException {
final Path
source = Paths.get("/tmp/source.txt"),
dest = Paths.get("/tmp/dest.txt");
Files.copy(source, dest); // May work.
Files.copy(source, dest); // Failure: FileAlreadyExistsException
}
No. 171
Expected exception test failure
Q: |
We reconsider:
Modify this code by catching the exception inside
|
A: |
We catch the exception in question:
Since we swallow the Destination file already exists java.lang.AssertionError: Expected exception: java.nio.file.FileAlreadyExistsException |