如何盖住挡块?

时间:2017-01-11 11:31:37

标签: java junit

我想对我的方法writeList()进行完整的代码覆盖,但我不知道如何覆盖catch块。

我的方法是:

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

}

2 个答案:

答案 0 :(得分: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();
}

注意:如果是FileNotFoundException,您只需将现有文件夹提供为pathOut,因为它是new FileOutputStream将投掷的案例之一一个FileNotFoundException,很容易重现。

答案 1 :(得分:-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);
        }      

}
}
}