This is the sample JUnit test code included in JUNIT4. It shows two cases: the type safe way and the dynamic way. I tried to use the type safe way.
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Some simple tests.
*/
public class SimpleTest extends TestCase {
protected int fValue1;
protected int fValue2;
SimpleTest(String string)
{
super(string);
}
@Override
protected void setUp() {
fValue1 = 2;
fValue2 = 3;
}
public static Test suite() {
/*
* the type safe way
*/
TestSuite suite= new TestSuite();
suite.addTest(
new SimpleTest("add") {
protected void runTest() { testAdd(); }
}
);
suite.addTest(
new SimpleTest("testDivideByZero") {
protected void runTest() { testDivideByZero(); }
}
);
return suite;
}
...
public static void main(String[] args) {
junit.textui.TestRunner.run(suite());
}
}
I'm not sure about this code snippet.
suite.addTest(
new SimpleTest("add") {
protected void runTest() { testAdd(); }
}
);
- How does new Simple("add") {...} works? I mean, how code block can follow the new operator?
- Why {...} block is needed? I tried without it, and I got no compilation/runtime errors.