0

我正在尝试为setTrailer()ErParser 类中的方法编写一个测试用例。setTrailer()有 try-catch 子句,并且在它的一个 catch 子句中,它会捕获NullPointerException. 我正在尝试为setTrailer()抛出和捕获 a的情况编写一个 Junit 测试NullPointerException,但测试用例一直失败。是因为我已经在方法本身中捕获了异常吗?我应该在测试用例中捕获异常吗?

测试用例:

public class TestERParser { 
    @Test(expected=NullPointerException.class)
    public void nullSetTrailer() {
        ERParser recCurrParse = new ERParser();
        recCurrParse.setTrailer(null);
    }   
}

ERParser 类中的 setTrailer() 方法:

public class ERParser {
private static final String TRAILER_E = "GRAND TOTAL";
private static final String TRAILER_R = "TRAILER";
public String trailerRecord; 

/**
 * Constructs an ERParser object.
 */
public ERParser() {
    this.trailerRecord = null;
    this.trailerVals = null;
}
/**
 * Populates the trailerRecord field with the summary (trailer) record of the input file.
 * @param file  Input file
 * @throws NullPointerException, FileNotFoundException, IOException
 */
public void setTrailer(File file) {
    try {
        FileReader fReader = new FileReader(file);
        BufferedReader bReader = new BufferedReader (fReader);
        String currLine = new String();
        readLoop:
            while (bReader.ready()) {
                currLine = bReader.readLine();
                if (currLine.contains(TRAILER_E) || currLine.contains(TRAILER_R)) {
                    break readLoop;
                }
            }
        this.trailerRecord = currLine.trim();
        System.out.println("From setTrailer(): " + this.trailerRecord);
        fReader.close();
        bReader.close();
    } catch (NullPointerException exception) {
        exception.printStackTrace();
    } catch (FileNotFoundException exception) {
        exception.printStackTrace();
    } catch (IOException exception) {
        exception.printStackTrace();
    }
}
}
4

3 回答 3

1

我们可以争论这个 catch 块是否意味着异常被处理。我认为仅仅打印堆栈跟踪并不能处理任何事情。在方法签名中添加 throws 子句并让客户决定如何处理异常可能会更好。

如果该方法是这样编写的,那么您可以按原样对其进行测试。如果这是第 3 方库,您将别无选择。

编写抛出异常的测试;成功意味着trailerRecord设置为null

您的代码还有另一个缺陷:在 finally 块中关闭流。您可能无法正确关闭写入的输入流。

于 2013-09-24T23:18:11.670 回答
1

正如您所怀疑的那样,您在代码中捕获了 NPE,并且它没有被传播。如果您希望您的用户捕捉到此异常,您应该删除此代码并用 throws 修饰您的方法到相应的类。

public void setTrailer(File file) throws Exception {
        FileReader fReader = new FileReader(file);
        BufferedReader bReader = new BufferedReader (fReader);
        String currLine = new String();
        readLoop:
            while (bReader.ready()) {
                currLine = bReader.readLine();
                if (currLine.contains(TRAILER_E) || currLine.contains(TRAILER_R)) {
                    break readLoop;
                }
            }
        this.trailerRecord = currLine.trim();
        System.out.println("From setTrailer(): " + this.trailerRecord);
        fReader.close();
        bReader.close();
}

由于您的代码现在抛出一个已检查的异常,您需要稍微更新您的 Junit 方法,以捕获已检查的异常

   @Test(expected=NullPointerException.class)
    public void nullSetTrailer() throws Exception {
        ERParser recCurrParse = new ERParser();
        recCurrParse.setTrailer(null);
    }   
于 2013-09-24T23:19:29.720 回答
0

在您的测试用例中,需要一个 NullPointerException 类。如果你抓住它,调用者类将不会得到它。因此,您可以删除 try/catch 块,也可以在打印 stacktrace 后重新抛出异常:

catch (NullPointerException exception) {
        exception.printStackTrace();
        throw new NullPointerException();
    }
于 2013-09-24T23:22:27.197 回答