Search code examples
javajunitjunit4testcasejunit-rule

How to print the value within the method in testcase?


I need to print the value within the method using the test case. My code is:

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

I need to print my value before throw a exception.


Solution

  • You can add a try and finally block and in finally you can print the message. I don't know if this could be done any better way than this:

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