Junit : how to show the report in a Jframe

user3464216

I have these files in my project :

enter image description here

I run the test in my Main class with :

    JUnitCore runner = new JUnitCore();
    runner.addListener(new TextListener(System.out));
    runner.run(AdditionTest.class);

I would like to show this report in a frame, how can I do ?

Javanator

There are several ways that you could do it. One way is to override RunListener. Obviously you will also have to create a JFrame, and I am assuming you just want to display the text, so you need a JTextArea. After that, you just have the tedious task of implementing all of RunListener's methods.

Lucky for you:

Main.java:

package pack;

import java.awt.Dimension;
import java.text.NumberFormat;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunListener;

import crea.AdditionTest;

public class Main extends RunListener
{
    private final JTextArea textArea    = new JTextArea();
    private final String    results     = new String();

    public static void main(String[] args)
    {
        new Main();
    }

    public Main()
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            @Override
            public void run()
            {
                JScrollPane jScrollPane = new JScrollPane(textArea);
                jScrollPane.setPreferredSize(new Dimension(500, 500));

                JFrame frame = new JFrame("Addition Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(jScrollPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });

        JUnitCore runner = new JUnitCore();
        runner.addListener(this);
        runner.run(AdditionTest.class);
    }

    @Override
    public void testFinished(Description description) throws Exception
    {
        super.testFinished(description);

        textArea.setText(results);
    }

    @Override
    public void testRunFinished(Result result)
    {
        printHeader(result.getRunTime());
        printFailures(result);
        printFooter(result);
    }

    @Override
    public void testStarted(Description description)
    {
        textArea.append(".");
    }

    @Override
    public void testFailure(Failure failure)
    {
        textArea.append("E");
    }

    @Override
    public void testIgnored(Description description)
    {
        textArea.append("I");
    }

    protected void printHeader(long runTime)
    {
        textArea.append("\n");
        textArea.append("Time: " + elapsedTimeAsString(runTime) + "\n");
    }

    protected void printFailures(Result result)
    {
        List<Failure> failures = result.getFailures();
        if (failures.size() == 0)
            return;
        if (failures.size() == 1)
            textArea.append("There was " + failures.size() + " failure:" + "\n");
        else
            textArea.append("There were " + failures.size() + " failures:"
                    + "\n");
        int i = 1;
        for (Failure each : failures)
            printFailure(each, "" + i++);
    }

    protected void printFailure(Failure each, String prefix)
    {
        textArea.append(prefix + ") " + each.getTestHeader() + "\n");
        textArea.append(each.getTrace());
    }

    protected void printFooter(Result result)
    {
        if (result.wasSuccessful())
        {
            textArea.append("\n");
            textArea.append("OK");
            textArea.append(" (" + result.getRunCount() + " test"
                    + (result.getRunCount() == 1 ? "" : "s") + ")" + "\n");

        }
        else
        {
            textArea.append("\n");
            textArea.append("FAILURES!!!");
            textArea.append("Tests run: " + result.getRunCount()
                    + ",  Failures: " + result.getFailureCount() + "\n");
        }
        textArea.append("\n");
    }

    protected String elapsedTimeAsString(long runTime)
    {
        return NumberFormat.getInstance().format((double) runTime / 1000);
    }
}

AdditionTest.java:

package crea;

import org.junit.Assert;
import org.junit.Test;

import pack.Addition;

public class AdditionTest
{
    @Test
    public void test5s()
    {
        int b = 5;
        for (int a = 10; a < 20; ++a)
            Assert.assertEquals(a + b, Addition.add(a, b));
    }

    @Test
    public void test6s()
    {
        int b = 6;
        for (int a = 10; a < 20; ++a)
            Assert.assertEquals(a + b, Addition.add(a, b));
    }

    @Test
    public void test2s()
    {
        int b = 2;
        for (int a = 10; a < 20; ++a)
            Assert.assertEquals(a + b, Addition.add(a, b));
    }

    @Test
    public void test8s()
    {
        int b = 8;
        for (int a = 10; a < 20; ++a)
            Assert.assertEquals(a + b, Addition.add(a, b));
    }
}

Addition.java:

package pack;

public class Addition
{
    public static int add(int a, int b)
    {
        if (a == 2 || b == 2)
            return -1;

        return a + b;
    }
}

Cheers :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

JUnit report to show test functionality, not coverage

From Dev

how to show Jframe before Joptiopane

From Dev

How to generate pdf report of junit test results?

From Dev

How to generate a basic JUnit/Selenium Report?

From Dev

NetBeans - How to show all Swing components in a JFrame?

From Dev

How to show original image and blurred image in JFrame?

From Dev

How to correctly show a JFrame after initializing it in a method

From Dev

How to show current date on a jframe title bar?

From Dev

How to show original image and blurred image in JFrame?

From Dev

How to pass list string to JLabel and show in JFrame

From Dev

How to show user input in JFrame box?

From Dev

How to Show output Jlabel another Jframe

From Dev

How to Show and Hide text in an SSRS report?

From Dev

How to show 4000 characters in RDLC report

From Dev

How to show data in multiple columns in report

From Dev

How to show 4000 characters in RDLC report

From Dev

Sitecore Analytics Report - How to show unique visitors

From Dev

how to show page number in jasper report

From Dev

How to force ant print junit's report with colour

From Dev

Jenkins testlink plugin how to fetch remote JUnit report

From Dev

How to show only JLabel hiding JFrame or any lower level container

From Dev

How to make a JFrame show advanced options when a button is clicked

From Dev

JUNIT test report

From Dev

Generate JUnit Test report

From Dev

Gradle Merge Junit Report

From Dev

Unable to show JFrame of library

From Dev

JFrame doesn't show

From Dev

Show the hidden JFrame

From Dev

Show JFrame in desire location