Java Reference
In-Depth Information
for ( int taskID # 0; taskID < 2; taskID !! )
for ( int activityID # 0; activityID < 7; activityID !! ){
activity # (Activity) problem.getActivity(
taskID, activityID);
value # (Double) expectedResults.get(
activity.getName());
assertEquals(value.doubleValue(),
activity.getActivation(), 1.0);
}
}
public static Test suite() {
return new TestSuite(TestScheduler.class);
}
public static void main (String[] args) {
junit.textui.TestRunner.run (suite());
}
}
Sidebar 3.2 JUnit test framework
JUnit is an automated testing framework for Java programs (www.junit.org). More
precisely, JUnit is a regression testing framework written by Erich Gamma and
Kent Beck. It is used by the developer who implements unit tests in Java. There
are plugins for several IDEs to make it easy the development and execution of
tests.
To test a Java program using JUnit is very easy. It is sufficient to define a class
that extends the class TestCase (defined in the framework), add public test
methods that check that the program behaves as expected, and run the test.
The JUnit framework is made up of three essential elements that can be used by
the programmer:
the assert methods,
class TestCase , and
class TestSuite .
The assert methods are used to check whether the results obtained from the
program are as expected; they can be used in the test methods. Class TestCase is
the container of a set of test methods, its test methods are executed by the JUnit
framework. Finally, class TestSuite is a collection of test cases, it can be used to run
in a single shot all the tests for a program.
For instance, to test a stack class, we can write the following test case:
// first we import the base class TestCase
import junit.framework.TestCase;
public class StackTester // the test case class must
extends TestCase { // extend class TestCase
// a constructor like this is required by
// JUnit framework to work properly
public void StackTester(String name) { super (name); }
// test methods must be "public void" and start with "test"
Search WWH ::




Custom Search