Do Assertions

There are two methods to use the assertion data defined in the JTestCase XML.
  • Use of the assertion feature provided by JTestCase (recommended).
  • Delegating assertion to JUnit assert methods.

JTestCase assertion facility

JTestCase provides the possibility to assert test cases with an ad-hoc API. This is the most straighforward way to assert and should be the preferred method in most situations. Here's an example.

In this example we assert that the code you called returns an integer that is lower than 3. You see that you can define not only the assert_value but also the assert_action in the XML. This gives you more flexibility in asserting data. Refer to the JavaDoc for more details on the method signatures and the possible assert_actions.

The corresponding part of the XML might look like the following:

<asserts>
  <assert_name="avariable" type="int" action="LT">3</assert>
</asserts>

And here's how you would use JTestCase assertion API in this case:

public class MyTest extends TestCase {

    private JTestCase _jtestcase = null;

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

    public void testSomething() {
        // get the test cases from XML
        Vector testCases = _jtestcase.getTestCasesInstancesInMethod("testSomething");

        // for each test case
        for (int i=0; i<testCases.size(); i++) {
            // retrieve name of test case
            TestCaseInstance testCase = (TestCaseInstance)testCases.elementAt(i);
            // assert a variable
            boolean succeed = testCase.
               assertTestCaseVariable("variable", <the_object_you_want_to_assert>);
            assertTrue(testCase.getFailReason(), succeed);
        }
    }
}

Delegation to JUnit assert methods

You can read the data and use it in the normal assertion methods of JUnit. The reason for this is that you may want to have more than one assertion for one variable (e.g. you want to test that your integer value is in a specific range).

The corresponding part of the XML might look like the following:

<asserts>
  <assertparam_name="avariable" type="int" action="LT">3</assert>
</asserts>

Therefore the assertions are hashed by their name and the specified action :

public class MyTest extends TestCase {

    private JTestCase _jtestcase = null;

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

    public void testSomething() {
        // get the test cases from XML
        Vector testCases = _jtestcase.getNameOfTestCases("testSomething");
        // 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 asserts for this test case
            MultiKeyHashtable asserts = testCase.getAssertParams();
            String[] keys1 = {"var1", "EQUALS"}
            int var1 = ((Integer)asserts.get(keys1)).intValue();
            String[] keys2 = {"var2", "EQUALS"}
            String var2 = (String)asserts.get(keys2);
            
            // Now you can use var1 and var2 parameters for
            // your  custom  assert function
        }
    }
}