Search code examples
javajunitjunit4junit3

How to create JUNIT Reports using runlistener?


I have been using JUNIT for my testing. I am able to run my testcases but the problem is how do i create JUNIT reports programatically using runlistener(Without using ANT or MAVEN). I am a bit stuck and would appreciate any guidance for the same.

Listener Implementation:-

JUnitCore junit = new JUnitCore();

junit.addListener(new MyJunitListener());

junit.run(AllEJBJunitTests.class);

How do i proceed with creation of report?


Solution

  • You can generate the HTML report using StringBuffer.

    String filePath = "File path where you want to keep your report";
    String reportFileName = "myReport.htm";
    
    JUnitCore junit = new JUnitCore();
    junit.addListener(new MyJunitListener());
    Result result = junit.run(AllEJBJunitTests.class);
    StringBuffer myContent = getResultContent(result, size);//Size represents number of AllEJBJunitTests class, suppose if you have 5 EJB classes then size is 5.
    writeReportFile(filePath + "/" + reportFileName, myContent);
    

    // This method will give you the HTML content.

    private StringBuffer getResultContent(Result result, int numberOfTestFiles)
           {
            int numberOfTest = result.getRunCount();
            int numberOfTestFail = result.getFailureCount();
            int numberOfTestIgnore = result.getIgnoreCount();
            int numberOfTestSuccess = numberOfTest - numberOfTestFail - numberOfTestIgnore;
            int successPercent = (numberOfTest != 0) ? numberOfTestSuccess * 100 / numberOfTest : 0;
            double time = result.getRunTime();
            StringBuffer myContent = new StringBuffer("<h1>Junitee Test Report</h1><h2>Result</h2><table border=\"1\"><tr><th>Test Files</th><th>Tests</th><th>Success</th>");
             if ((numberOfTestFail > 0) || (numberOfTestIgnore > 0)) {
               myContent.append("<th>Failure</th><th>Ignore</th>");
             }
    
             myContent.append("<th>Test Time (seconds)</th></tr><tr");
             if ((numberOfTestFail > 0) || (numberOfTestIgnore > 0)) {
               myContent.append(" style=\"color:red\" ");
             }
             myContent.append("><td>");
             myContent.append(numberOfTestFiles);
             myContent.append("</td><td>");
             myContent.append(numberOfTest);
             myContent.append("</td><td>");
             myContent.append(successPercent);
             myContent.append("%</td><td>");
             if ((numberOfTestFail > 0) || (numberOfTestIgnore > 0)) {
               myContent.append(numberOfTestFail);
               myContent.append("</td><td>");
               myContent.append(numberOfTestIgnore);
               myContent.append("</td><td>");
             }
    
             myContent.append(Double.valueOf(time / 1000.0D));
             myContent.append("</td></tr></table>");
             return myContent;
           }
    

    // This will generate the report in the particular directory which you have mentioned.

    private void writeReportFile(String fileName,StringBuffer reportContent){
            FileWriter myFileWriter = null;
            try {
                myFileWriter = new FileWriter(fileName);
                myFileWriter.write(reportContent.toString());
            }
            catch (IOException e) {
    
            }
            finally {
                if (myFileWriter!=null) {
                    try {
                        myFileWriter.close();
                    }
                    catch (IOException e) {
    
                    }
                }
            }
        }
    

    Cheers !!