Access Test Data

In order to access the test data defined in the XML there are some simple API methods of JTestCase. The following example shows how to read test case related paramters in a JUnit test case.

How to get your test data

  • private JTestCase _jtestcase = null;

    Declaration of a JTestCase instance.
  • _jtestcase = new JTestCase(dataFile, "MyTest");

    Instantiation of JTestCase. The parameters are the name of the JTestCase XML file and the name of the test class (remember that this has to match the name attribute of the class tag in the XML).
  • Vector testCases = _jtestcase.getTestCasesInstancesInMethod("testSomething");

    Gets a vector with all TestCaseInstance objects of test cases for the specified test method. You have now the possibility to iterate over the test cases.
  • HashMap params = testCase.getTestCaseParams();

    Gets the test data from the JTestCase XML file in a hashtable. You can access the parameters in the hashtable with the parameter name as key.

Here'a a concrete example:

public class MyTest extends TestCase {

    private JTestCase _jtestcase = null;

    public MyTest(String name) throws Exception {
        super(name);
        String dataFile = "test-data.xml";
        _jtestcase = new JTestCase(dataFile, "MyTest");
     }

    public void testSomething() {
        // get the test cases from XML
				try{
        Vector testCases = _jtestcase.getTestCasesInMethod("testMethod");
          // for each test case
          for (int i=0; i<testCases.size(); i++) {
            // retrieve name of test case
            TestCaseInstance testCase = (TestCaseInstance)testCases.elementAt(i);
            
            // get hashed params for this test case
            HashMap params = testCase.getTestCaseParams();
            int var1 = ((Integer)params.get("var1")).intValue();
            String var2 = (String)params.get("var2");
            
            // Now comes to what we need to test.
          }
				}
				catch{JTestCaseException e) {
				  fail("JTestCase framework fail");
				}
    }
}

Using ParamGroups

  • "Normal" parameters are references by their name, e.g. String var2 = (String)params.get("var2")
  • A parameter embedded in a paramgroup will be references with the paramgroup name and the name separated by a slash, e.g. String var2 = (String)params.get("paramgroup/var2"). If the paramgroup is itself part of a paramgroup the name of the parent has to be added accordingly.
  • Parameters in complex assertaction have to be extracted as the complex type first, e.g. Hashtable var2 = (Hashtable)params.get("var2"). The values can than be extracted from the complex type.