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 Calculator.java:

package sample.tests;

public class Calculator {

  public Calculator() {
    super();
  }

  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: CalculatorTest.java and test-data.xml.
It is assumed that the classes are in the sample/tests directory.

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

import java.io.FileNotFoundException;
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=
 "http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation=
 "http://jtestcase.sourceforge.net/dtd/jtestcase2.xsd">
 <class name="aclass">
  ...
 </class>
 <class name="anotherclass">
  ...
 </class>
 ....
</tests>

 

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) {
    super(name);

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

  }

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

 

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) {
      e.printStackTrace();
      fail("error parsing xml file for calculate method " + METHOD);
    }
    

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

 

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
          .elementAt(i);

      try {


  <test-case name="test1">
    ......
  </test-case>
	

 

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">
      <params>
        <param name="var1" type="int">1</param>
        <param name="var2" type="int">1</param>
        <param name="opt" type="String">+</param>
      </params>
    </test-case>

 

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)));


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