I have these files in my project :
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 ?
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.
Comments