I am having trouble when I try to reorganize my code. I am using reflection combined with swing GUI to run a test class's tests. As you can see below I am doing everything in one class in the constructor which is very bad. So I created a new class that would create the GUI and handle the buttons' actions.
The thing with the program is that it checks if there is a setUp and tearDown methods in the test class. When I have everything in my constructor, it looks and notices these first and I am able to continue. But when I moved everything to another class. The program for some reason find setUp and tearDown last which failes my program. Anyone know the reason to my problem?
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.*;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class MyUnitClass {
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container pane = frame.getContentPane();
JPanel panel = new JPanel();
// First panel
JButton button = new JButton("Run tests");
final JTextField field = new JTextField();
pane.add(panel, BorderLayout.PAGE_START);
panel.setBackground(new Color(102,99,98));
field.setPreferredSize(new Dimension(300, 25));
panel.add(field);
panel.add(button);
// Second panel
JPanel panel2 = new JPanel();
final JTextArea area = new JTextArea();
area.setEditable(false);
pane.add(panel2, BorderLayout.CENTER);
panel2.setPreferredSize(new Dimension(800, 400));
area.setPreferredSize(new Dimension(800, 400));
panel2.add(area);
// Button click
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
String str = field.getText();
int testSucced = 0;
int testFail = 0;
int testFailException = 0;
try{
Class<?> c = Class.forName(str);
int interfaceImplemented = 0;
if(TestClass.class.isAssignableFrom(c)){
interfaceImplemented = 1;
}
if(interfaceImplemented == 1)
{
Object t = c.newInstance();
Method[] allMethods = c.getDeclaredMethods();
int setUpInt = 0, tearDownInt = 0;
for (Method m : allMethods) {
String mName = m.getName();
if(mName == "setUp")
setUpInt = 1;
if(mName == "setUp")
tearDownInt = 1;
}
Method setUp = null;
Method tearDown = null;
for (Method m : allMethods) {
String mName = m.getName();
if(mName == "setUp")
setUp = m;
if(mName == "tearDown")
tearDown = m;
if (!mName.startsWith("test")) {
continue;
}
int fail;
Object d = null;
if(setUpInt == 1 && tearDownInt == 1) {
try {
fail = 0;
setUp.invoke(t);
d = m.invoke(t);
tearDown.invoke(t);
} catch (InvocationTargetException x) {
fail = 1;
area.append(mName + ": " + "FAIL Generated a " + x + "\n");
x.getCause().printStackTrace();
testFailException++;
}
if(fail == 0){
area.append(mName + ": " + d + "\n");
if(d.equals(true))
testSucced++;
else if (d.equals(false))
testFail++;
}
}
else if(setUpInt == 1 && tearDownInt != 1) {
try {
fail = 0;
setUp.invoke(t);
d = m.invoke(t);
} catch (InvocationTargetException x) {
fail = 1;
area.append(mName + ": " + "FAIL Generated a " + x + "\n");
x.getCause().printStackTrace();
testFailException++;
}
if(fail == 0){
area.append(mName + ": " + d + "\n");
if(d.equals(true))
testSucced++;
else if (d.equals(false))
testFail++;
}
}
else if(setUpInt != 1 && tearDownInt == 1) {
try {
fail = 0;
d = m.invoke(t);
tearDown.invoke(t);
} catch (InvocationTargetException x) {
fail = 1;
area.append(mName + ": " + "FAIL Generated a " + x + "\n");
x.getCause().printStackTrace();
testFailException++;
}
if(fail == 0){
area.append(mName + ": " + d + "\n");
if(d.equals(true))
testSucced++;
else if (d.equals(false))
testFail++;
}
}
else if(setUpInt != 1 && tearDownInt != 1) {
try {
fail = 0;
d = m.invoke(t);
} catch (InvocationTargetException x) {
fail = 1;
area.append(mName + ": " + "FAIL Generated a " + x + "\n");
x.getCause().printStackTrace();
testFailException++;
}
if(fail == 0){
area.append(mName + ": " + d + "\n");
if(d.equals(true))
testSucced++;
else if (d.equals(false))
testFail++;
}
}
}
}
} catch (ClassNotFoundException x) {
x.printStackTrace();
JOptionPane.showMessageDialog(frame,
"You need to enter a testclass",
"Error",
JOptionPane.WARNING_MESSAGE);
} catch (IllegalAccessException x) {
x.printStackTrace();
} catch (InstantiationException x) {
x.printStackTrace();
}
area.append("\n" + testSucced + " tests succeded\n");
area.append(testFail + " tests failed\n");
area.append(testFailException + " tests failed because of an exception\n");
}
});
// Third panel
JPanel panel3 = new JPanel();
JButton button2 = new JButton("Clear");
pane.add(panel3, BorderLayout.PAGE_END);
panel3.setBackground(new Color(102,99,98));
panel3.add(button2);
// Second button click
button2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{
area.setText("");
}
});
frame.pack();
frame.setVisible(true);
}
}
Interface class:
public interface TestClass {
}
The test class that I am testing with:
public class Test1 implements TestClass {
private MyInt myInt;
public Test1() {
}
public void setUp() {
myInt=new MyInt();
}
public void tearDown() {
myInt=null;
}
//Test that should succeed
public boolean testInitialisation() {
return myInt.value()==0;
}
//Test that should succeed
public boolean testIncrement() {
myInt.increment();
myInt.increment();
return myInt.value()==2;
}
//Test that should succeed
public boolean testDecrement() {
myInt.increment();
myInt.decrement();
return myInt.value()==0;
}
//Test that should fail
public boolean testFailingByException() {
myInt=null;
myInt.decrement();
return true;
}
//Test that should fail
public boolean testFailing() {
return false;
}
}
The class which the test class uses:
public class MyInt {
private int val;
public MyInt() {
val=0;
}
public void increment() {
val++;
}
public void decrement() {
val--;
}
public int value() {
return val;
}
}
Aucun commentaire:
Enregistrer un commentaire