如何在testcase中的方法中打印值?

时间:2012-12-19 12:00:58

标签: java junit junit4 testcase junit-rule

我需要使用测试用例打印方法中的值。 我的代码是:

public void testgetAllRatingElementsWeekNo() throws Exception
{
    String METHOD_NAME = "testgetAllRatingElementsWeekNo";
    log.entering(CLASS_NAME, METHOD_NAME);

    populateForTestMethodValues("1");
    populateWeekOfList();
    int allRatingElementsWeekNo = weeklyDlvyInstancesDashboardReportForm.getAllRatingElementsWeekNo();
    System.out.println("allRatingElementsWeekNo :" + allRatingElementsWeekNo );
    assertEquals("testgetAllRatingElementsWeekNo is Not Greater than Zero: ", allRatingElementsWeekNo > 0, allRatingElementsWeekNo);

    log.exiting(CLASS_NAME, METHOD_NAME);
}

我需要在抛出异常之前打印我的值。

1 个答案:

答案 0 :(得分:0)

您可以添加try和finally块,最后可以打印消息。我不知道这是否可以做得比这更好:

public void testgetAllRatingElementsWeekNo() throws Exception
{
try {
    String METHOD_NAME = "testgetAllRatingElementsWeekNo";
    log.entering(CLASS_NAME, METHOD_NAME);

    populateForTestMethodValues("1");
    populateWeekOfList();
    int allRatingElementsWeekNo = weeklyDlvyInstancesDashboardReportForm.getAllRatingElementsWeekNo();
    System.out.println("allRatingElementsWeekNo :" + allRatingElementsWeekNo );
    assertEquals("testgetAllRatingElementsWeekNo is Not Greater than Zero: ", allRatingElementsWeekNo > 0, allRatingElementsWeekNo);
} finally {
    log.exiting(CLASS_NAME, METHOD_NAME);
}
}