Control Unit Tests
From JWic
This article describes how you can easily test your controls using JUnit.
Setup the Test-Environment
Before you can run tests, you must add a few configuration files that are used to setup the JWicRuntime and the loggers. Start by creating a folder "test" in your project and copy the files jwic-setup.xml and log4j.properties from your WEB-INF folder into it. You may now customize the logging settings if required.
Second step is to create a file named testenv.properties in the classpath of your tests. We think that it is a good practice to create a seperated source folder for the tests , i.e. named src_tests. There you can create the file with the following content:
# Test-Environment setup for the jWic Base Tests rootpath=test # The log4j.properties file log4j.properties=test/log4j.properties # The path where the runtime stores the serialized sessions jwic-setup.xml=test/jwic-setup.xml
Now you are ready to write unit tests.
Create a ControlTest
Simply create a new class that extends de.jwic.test.ControlTestCase. This class is a TestCase object that can be run with JUnit. It does create a new application with a Page as root object. Then it calls the abstract method createControl(IContainer) where the control that is under test must be created. Furthermore it offers methods to manipulate the control to simulate a browser.
Here is the sample code for a test:
public class MyControlTest extends ControlTestCase {
private MyControl myControl;
/* (non-Javadoc)
* @see de.jwic.test.ControlTestCase#createControl(de.jwic.base.IControlContainer)
*/
public Control createControl(IControlContainer container) {
myControl = new MyControl(container);
return myControl;
}
public void testAlert() {
myControl.alert();
assertTrue(myControl.isAlerted());
}
}
You can already run this test. In the above sample, the method under test is called directly as a controller would do. But it is also possible to simulate the access as it happens through the browser. With the method sendAction(..), you can simulate the click of an user on a link generated by the createActionURL method. The test code looks like this:
public void testUserAction() {
myControl.addElement("A");
myControl.addElement("B");
// simulate user action
sendAction(myControl, "selectElement", "A");
assertEquals("A", myControl.getSelectedKey());
}
Controls that work with Field objects can also be tested using the updateField(..) methods provided by the ControlTestCase. Important in this case is that after all fields have been updated, the method updateDone() must be called. The code to test a calculator control could looks like this:
public void testUserAction() {
updateField(myControl, "value1", "2");
updateField(myControl, "value2", "3");
updateField(myControl, "op", "+");
updateDone(); // process value changed listeners
// simulate user action
sendAction(myControl, "calc", "");
assertEquals(5, myControl.getResult());
}
You can test simple controls as well as complex control containers.

