Junit:如何在Jframe中显示报告

时间:2014-11-20 20:55:44

标签: java junit

我的项目中有这些文件:

enter image description here

我在我的Main课程中运行测试:

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

我想在一个框架中显示此报告,我该怎么办?

1 个答案:

答案 0 :(得分:0)

有几种方法可以做到。一种方法是覆盖RunListener。显然你还必须创建一个JFrame,我假设你只想显示文本,所以你需要一个JTextArea。在那之后,您只需执行所有RunListener's方法的繁琐工作。

幸运的是你:

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

干杯:)