Descriptive Structure
The descriptive structure of a test case is usually depicted in one class that extends the com.prosyst.tee.tfw.asrt.TestSuite class which implements the com.prosyst.tee.tfw.asrt.Test interface. This class represents the root element of the test case tree. It holds information about the name and the description of the test case. It also contains a list of tests that are logically connected and should be executed in the same order as they appear in this class.
Test case nodes can be directly instantiated in the constructor of the root class. Use the add method of the TestSuite class to add them in the way you want to be executed. There are three options for adding the desired tests:
Adding test methods:
String className = TESTED_PACKAGE_NAME + "MyFirstTestCaseClass";
add(new TestMethod("myMethod()", className + "#testMyMethod", TEST_METHOD_DESCRIPTION));
add(new TestMethod("mySecondMethod(int)", className + "#testMySecondMethod",
ANOTHER_TEST_METHOD_DESCRIPTION));
Only test methods with names starting with "test" will be called when the TestClass is executed.
Adding a whole test class:
add(new TestClass(TESTED_PACKAGE_NAME + "MySecondTestCaseClass"));
Adding a test suite:
TestSuite ts = new TestSuite("MyTestSuite", "Provides some simple test methods");
{
String className = TESTED_PACKAGE_NAME + "MyThirdTestCaseClass";
ts.add(new TestMethod(className + "#firstmethod"));
ts.add(new TestMethod(className + "#secondmethod"));
}
//Adding the test suite to the test case tree
add(ts);
This way of grouping test methods allows you to add methods which names do not begin with test.
TEE uses reflection to create instances of the test case class(es) listed in the descriptor structure and to invoke the declared test methods.
Testing Code
The testing code represents the test case logic and consists of one or more classes. The testing code is loaded when the descriptive structure is executed.
The OSGi-aware test cases are written to evaluate some conditions in a specific environment. The TEE allows you to define these environments through the constructors of the class that contains testing code. For example, to use the OSGi functionality provided by the OSGi framework you can pass to the constructor of your class the BundleContext interface. During the execution of the testing code, the TEE examines the constructors of the class using reflection to make a list of the constructors with arguments satisfied by the environment. Then it creates an instance of the class using the constructor with the most arguments satisfied by the environment.
Each class part of the testing code should extend either the com.prosyst.tee.tfw.asrt.TestTemplate or the com.prosyst.tee.tfw.asrt.ExceptionTestTemplate class.
The TestTemplate class extends the com.prosyst.tee.tfw.asrt.Assert class thus providing direct calls to all assertion methods defined for this test model. These methods are used to validate that certain tested functionality works as expected. During the execution of the testing code some assertions might fail. Then the AssertionFailedError is thrown allowing you to precisely define the reason for the failure in the testing code. If you want to assert that the expected and the actual results of a tested functionality are the same, use one of the assertEquals methods of the Assert class. To assert whether a reference to an object is null or not, use one of the assertNull or the assertNotNull assertion methods. In case you want to check whether a specific condition is marked with true or false flag, use one of the assertTrue methods or one of the assertFalse methods of the Assert class. To assert that two objects are equal or not, use one of the assertSame methods or one of the assertNotSame methods. The fail(String message) method is used to mark the places in the testing code where an AssertionFailedError should be immediately thrown.
All assertions should be made only in the main execution thread of the test case.
The example that follows demonstrates the use of the assertEquals(String message, int expected, int actual) method:
Using assertion methods:
public void testFailed() {
assertEquals("Demonstrates how to fail a test case.", -1, +1);
}
There are four levels that a test case can achieve when it is executed.
The TestTemplate class also extends the com.prosyst.tee.tfw.asrt.Fixture class thus providing empty implementations of the setUp() and tearDown() methods. When only a single test method from the test class is executed, these methods are called before and after the test method. When a whole test class is processed these methods are called at the beginning and at the end of the class.
The ExceptionTestTemplate abstract class extends the TestTemplate class to provide a template for writing test cases that verify that a single exception is thrown. You should simply extend this class and pass to the constructor the exception that must be verified. Use the performTest() abstract method to provide your testing code.
To use this class simply extend it and place your testing code in the abstract performTest method. The exception which must be verified is passed to the constructor of this class. Usually extending classes must provide a constructor that passes an exact exception class up to the constructor of this class.