rzucanie wyjątków w testach

0

Witam.
Podczas wywoływania metody toTable która rzuca IOExceptions w głownej metodzie klasy nie potrzebuję używać bloku try catch.
Wszystko działa. Problem pojawił się przy testowaniu JUnit. Jeśli wywołuję w teście tą metodę muszę użyć bloku try catch ponieważ dostaję error : unreported exception IOException; must be caught or declared to be thrown jeśli znowu użyję try - catch po odpaleniu testu dostaję error : Forked Java VM exited abnormally. Please note the time in the report does not reflect the time until the VM exit.
junit.framework.AssertionFailedError
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153). Czy ktoś wie o co chodzi ?

0

Bez kodu nie bardzo wiadomo o co ci chodzi. Generalnie nie rób try-catch w teście tylko rzucaj wyjątek dalej. Jak poleci to test się wysypie i powinien. A błąd wskazuje na to że test się wysypał na asercji.

0

Chodzi o tą metodę

public static void toTable(String fileName) throws IOException
    {
        /** variable which stores information about an error */
        boolean error = false;
        // reading file
        try
        {   
            FileReader fileReader = new FileReader(fileName);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            
            /** variable which stores one line of text from input file */
            String line = bufferedReader.readLine();
            
            // saving text from file to table
            while(line != null) 
            {   
                table = table + line;
                line = bufferedReader.readLine();
            } 
            bufferedReader.close();
        }// if file can't be found
        catch(FileNotFoundException e)
        {
            error = true;
            System.out.println("Wrong file name!!!");
        }
        
        // if error will occur, close program 
        if(error == true)
        {
            System.exit(0);
        }
       
    }
    

w mainie tej klasy wystarczy wywołuję ją w ten sposób:
toTable(outputFileName);

natomiast w testach juz tak się nie da ...

@Test
    public void testToTable()
    {
        

            auxObj.setTable("Text to encrypt");
            auxObj.setCharAm(15);
            auxObj.setMatrixSize(4);
           
            auxObj.toTable("out");
        
    }

a przy ty catch dostaję tego errora

@Test
    public void testToTable()
    {
        

            auxObj.setTable("Text to encrypt");
            auxObj.setCharAm(15);
            auxObj.setMatrixSize(4);
           try{
            auxObj.toTable("out");
           }
           catch(IOException e)
           {
            }
    }

Forked Java VM exited abnormally. Please note the time in the report does not reflect the time until the VM exit.
junit.framework.AssertionFailedError
at org.netbeans.core.execution.RunClassThread.run(RunClassThread.java:153)

0
  1. Kłamiesz, z maina musisz rzucać IOException żeby ci ten kod działał
  2. W teście też rzucaj IOException z metody testowej po prostu
  3. Zapewne geniuszu ten System.exit() ci rozwala vmke... W ogóle zabicie VMki z jakiejś randomowej metody to jest perełka i wtf tygodnia. Brak mi słów.
0

w mainie wywołuję tą metodę w ten sposób :

toTable(outputFileName);
0

tak na marginesie czemu ta metoda toTable jest static? mógłbyś pokazać całą klasę ,bo średnio to wygląda

0

Słabo mi. Wody. Geniuszu, tylko że twój main wyglada tak:

public static void main(String[] args) THROWS LISTA_WYJĄTKÓW

Czyż nie?

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