Getting Started

You can also find this example in the example directory of the download bundle. release bundle

Suppose you need to test the following class called

package sample.tests;

public class Calculator {

  public Calculator() {

  public int calculate(int var1, int var2, String opt) {
    if (opt.equals("+"))
      return var1 + var2;
    if (opt.equals("-"))
      return var1 - var2;
    return 0;


To use jtestcase, you'll need to create two files: and test-data.xml.
It is assumed that the classes are in the sample/tests directory.

JTestCase instance to be used in this example The xml data file to be used in this example
package sample.tests;

import java.util.HashMap;
import java.util.Vector;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

import org.jtestcase.JTestCase;
import org.jtestcase.JTestCaseException;
import org.jtestcase.TestCaseInstance;

public class CalculatorTest extends TestCase {

<?xml version="1.0" encoding="utf-8"?>
<tests xmlns:xsi=
 <class name="aclass">
 <class name="anotherclass">


Read the XML file with the test data and build the JTestCase instance. In the XML file, declare your tested class.
   * JTestCase instance to be used in this example
  private JTestCase _jtestcase = null;

  public CalculatorTest(String name) {

    String dataFile = "sample/tests/test-data.xml";
    try {
      _jtestcase = new JTestCase(dataFile, "Calculator");
    catch (FileNotFoundException e) {
    catch (JTestCaseException e) {


  <class name="Calculator">
    <method name="amethod">
    <method name="anothermethod">


Get the Vector of the test cases defined for this method In the XML file, declare your test case using a (repetible) test-case tag within a method tag.
  public void testCalculate() {

    if (_jtestcase == null)
      fail("couldn't read xml definition file");

    final String METHOD = "calculate";
    Vector testCases = null;
    try {
      testCases = _jtestcase.getTestCasesInstancesInMethod(METHOD);
    catch (JTestCaseException e) {
      fail("error parsing xml file for calculate method " + METHOD);

  <method name="calculate">
    <test-case name="test1">
    <test-case name="test2">


Loop over the defined test cases. the defined test case.
    for (int i = 0; i < testCases.size(); i++) {

      // Retrieve name of test case

      TestCaseInstance testCase = (TestCaseInstancetestCases

      try {

  <test-case name="test1">


Get the params for this test case instance. In the XML file, define the test case params.
        // Get hashed params for this test case
        HashMap params = testCase.getTestCaseParams();

        Integer var1Int = (Integerparams.get("var1");
        Integer var2Int = (Integerparams.get("var2");
        String opt = (Stringparams.get("opt");

        int var1 = var1Int.intValue();
        int var2 = var2Int.intValue();

    <test-case name="test1">
        <param name="var1" type="int">1</param>
        <param name="var2" type="int">1</param>
        <param name="opt" type="String">+</param>


Now comes to what we need to test.
the defined test case.
        // All testing related to Calculate here
        Calculator calculator = new Calculator();
        int result = calculator.calculate(var1, var2, opt);

<!-- the class under test is         -->
<!-- defined in the test case  class -->


Assert your defined tests using the JTestCase assertion API define your per-test-case assearts using the assert tag.
        // Asserting result
        boolean succeed = testCase.assertTestVariable("result",
            (new Integer(result)));

         <assert name="result" type="int" action="EQUALS">10</assert>