Próbuję napisać test niepoprawnej deserializacji danych z pliku zip - po prostu próbuję odczytać plik który nie istnieje. Gdy włączam test, w konsoli wyrzuca mi wyjątek, następnie wypisuje informację że ten właśnie wyjątek był oczekiwany ale nie został zarejestrowany. Potrzebuję podpowiedzi co robię źle.
@Test
public void odczytDanych_Unsuccessful_throwsNoSuchFileException() {
//given
Ewidencja ewidencja = new Ewidencja();
//then
Assertions.assertThrows(NoSuchFileException.class, () -> ewidencja.odczytDanych("tenPlikNieIstnieje.zip"));
}
Treść konsoli
java.nio.file.NoSuchFileException: C:\tenPlikNieIstnieje.zip
[...]
org.opentest4j.AssertionFailedError: Expected java.nio.file.NoSuchFileException to be thrown, but nothing was thrown.
Treść testowanej metody
public Ewidencja odczytDanych(String filename) throws IOException, FileNotFoundException, ClassNotFoundException{
String filepath = "C:\\";
String extension = filename.substring(filename.length() - 2);
Ewidencja newEwid = new Ewidencja();
try {
byte[] buffer = new byte[1024];
String dest = filename.substring( 0, filename.indexOf("."));
File file = new File(filepath + dest);
file.mkdir();
dest = filepath + dest;
InputStream fi = Files.newInputStream(Paths.get(filepath + filename));
BufferedInputStream bi = new BufferedInputStream(fi);
if(extension.equalsIgnoreCase("ip")) {
ZipInputStream gzi = new ZipInputStream(bi);
ZipEntry entry;
while ((entry = gzi.getNextEntry()) != null) {
Path destPath = Paths.get(dest).resolve(entry.getName());
Files.copy(gzi, destPath, StandardCopyOption.REPLACE_EXISTING);
}
}else if(extension.equalsIgnoreCase("gz")) {
GzipCompressorInputStream gzi = new GzipCompressorInputStream(bi);
TarArchiveInputStream ti = new TarArchiveInputStream(gzi);
ArchiveEntry entry;
while ((entry = ti.getNextEntry()) != null) {
Path destPath = Paths.get(dest).resolve(entry.getName());
Files.copy(ti, destPath, StandardCopyOption.REPLACE_EXISTING);
}
}
else {
return null;
}
List<CompletableFuture<Pracownik>> cfList = new ArrayList<CompletableFuture<Pracownik>>();
ExecutorService executorService = Executors.newFixedThreadPool(8);
File folder = new File(dest);
File[] listOfFiles = folder.listFiles();
final Pracownik[] thisFileOut = {null};
for (File pracownik : listOfFiles) {
CompletableFuture<Pracownik> thisPrac = CompletableFuture.supplyAsync(() -> {
try {
thisFileOut[0] = odczytPracownika(pracownik.getAbsolutePath());
} catch (FileNotFoundException e) {
System.out.print("FileNotFoundExp");
e.printStackTrace();
} catch (IOException e) {
System.out.print("IOExp");
e.printStackTrace();
}catch (ClassNotFoundException e){
System.out.print("ClassNotFoundExp");
e.printStackTrace();
}
return thisFileOut[0];
}, executorService);
cfList.add(thisPrac);
thisPrac.get();
}
for(CompletableFuture<Pracownik> item : cfList){
newEwid.addPracownik(item.get());
}
executorService.shutdown();
Files.walk(Paths.get(dest))
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (NoSuchFileException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return newEwid;
}