2017-01-11 4 views
0

私のメソッドwriteList()の完全なコードカバレッジを持っていますが、キャッチブロックのカバー方法はわかりません。キャッチブロックをカバーする方法は?

私の方法は次のとおりです。私はあなただったら

public class ListOfNumbers { 


    //...another code... 


    public void writeList() { 
     try (FileOutputStream inputStream = new FileOutputStream(this.pathOut); 
       OutputStreamWriter outputStreamWriter = new OutputStreamWriter(inputStream); 
       PrintWriter out = new PrintWriter(outputStreamWriter)) { 
      for (int i = 0; i < this.numbers.size(); i++) { 
       out.println("Value at: " + i + " = " + this.numbers.get(i)); 
      } 
     } catch (final ArrayIndexOutOfBoundsException e) { 
      ListOfNumbers.LOGGER.error("Caught ArrayIndexOutOfBoundsException: " + e.getMessage(), 
        e); 
     } catch (final FileNotFoundException e) { 
      ListOfNumbers.LOGGER.error("Caught FileNotFoundException: " + e.getMessage(), e); 
     } catch (final IOException e) { 
      ListOfNumbers.LOGGER.error("Caught IOException: " + e.getMessage(), e); 
     } 
    } 

} 
+1

「カバー」とは何ですか?なぜ?してください。 –

+1

まず、なぜArrayIndexOutOfBoundsExceptionをキャッチしますか?このような 'RuntimeException'をキャッチすることは一般的にひどい習慣であり、それは設計上の問題であることを示します –

+1

[例外の単体テストを行う方法?](http://stackoverflow.com/questions/3305453/how-例外の対象となる単体テスト) – Tom

答えて

1

は、それを行うには、私は別の方法にnumbersを記述したコードを移動することで、それはより多くのテスト可能にするために私のコードを書き換えるだろう、この新しいメソッドを模擬して、Mockitoを使用して必要なものをスローします。

だからあなたのコードは、そのようなことが考えられます。

public void writeList() { 
    try (FileOutputStream inputStream = new FileOutputStream(this.pathOut); 
      OutputStreamWriter outputStreamWriter = new OutputStreamWriter(inputStream); 
      PrintWriter out = new PrintWriter(outputStreamWriter)) { 
     // Delegate the way to write the numbers to this new method 
     writeNumbers(out); 
    } catch (final ArrayIndexOutOfBoundsException e) { 
     ListOfNumbers.LOGGER.error(
      "Caught ArrayIndexOutOfBoundsException: " + e.getMessage(), e 
     ); 
    } catch (final FileNotFoundException e) { 
     ListOfNumbers.LOGGER.error("Caught FileNotFoundException: " + e.getMessage(), e); 
    } catch (final IOException e) { 
     ListOfNumbers.LOGGER.error("Caught IOException: " + e.getMessage(), e); 
    } 
} 

/** 
* The new method that is protected here but could also be package protected 
* but cannot be private to be able to override it. 
*/ 
protected void writeNumbers(PrintWriter out) { 
    for (int i = 0; i < this.numbers.size(); i++) { 
     out.println("Value at: " + i + " = " + this.numbers.get(i)); 
    } 
} 

次に、あなたのユニットテストは、次のようになります。

@Test 
public void causeAIOException() { 
    ListOfNumbers lon = // Create your instance here 
    // Create a Spy to be able to mock the method writeNumbers 
    ListOfNumbers listOfNumbers = Mockito.spy(lon); 
    // This will make any call to writeNumbers throw a IOException 
    Mockito.doThrow(IOException.class).when(listOfNumbers) 
     .writeNumbers(Matchers.any(PrintWriter.class)); 
    // Call the method on the spy 
    listOfNumbers.writeList(); 
} 

NB:FileNotFoundExceptionの場合、あなたは、単に既存のフォルダを提供することができますnew FileOutputStreamFileNotFoundExceptionを投げるケースの1つであるため、pathOutとなり、再生が容易です。

-1
class ListOfNumbers { 

    public void writeList() throws ArrayIndexOutOfBoundsException,FileNotFoundException,IOException { 
     try (FileOutputStream inputStream = new FileOutputStream(this.pathOut); 
       OutputStreamWriter outputStreamWriter = new OutputStreamWriter(inputStream); 
       PrintWriter out = new PrintWriter(outputStreamWriter)) { 
      for (int i = 0; i < this.numbers.size(); i++) { 
       out.println("Value at: " + i + " = " + this.numbers.get(i)); 
      } 

    } 

} 


public class Main{ 
public static void main(String [] args){ 
    try{ 
    ListOfNumbers list=new ListOfNumbers(); 
try{ 
    list.writeList(); 
} 
    } catch (ArrayIndexOutOfBoundsException e) { 
      ListOfNumbers.LOGGER.error("Caught ArrayIndexOutOfBoundsException: " + e.getMessage(), 
        e); 
     } catch (FileNotFoundException e) { 
      ListOfNumbers.LOGGER.error("Caught FileNotFoundException: " + e.getMessage(), e); 
     } catch (IOException e) { 
      ListOfNumbers.LOGGER.error("Caught IOException: " + e.getMessage(), e); 
     }  

} 
} 
} 
+0

ここで行ったことを説明するために答えを更新する可能性はありますか?コードのみの回答は、他の人にとって役に立たない傾向があります。 – Tom

+0

もう一度コードをチェックしてください。いくつかの問題のためにコンパイルできません。 – Tom

関連する問題