JUnit - test na NoSuchFileException

0

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;
    }
1

Problem to już jest z kodem tej metody od samego początku. Jest tak z 10 razy za długa. Weź ją potnij jak człowiek. Ja tam widze miejsce na 6 różnych funkcji tak na pierwszy rzut oka.
Wracając do pytania: przeciez łapiesz wszystkie wyjątki i tylko printujesz stack trace więc nie wiem czego się spodziewasz? o_O Ta metoda nie rzuca żadnego wyjątku.
A ten throws ClassNotFoundException to jakis majstersztyk :D

1 użytkowników online, w tym zalogowanych: 0, gości: 1