JUnit is a testing program which focuses on Java Units, which are in this case Java Classes. The test cases test the functionality of classes to ensure that for a given input the expected output is obtained. In the event that a change is made which affects the functional nature of a Class such that the desired output is no longer obtained, then the Unit Test will fail.
Unit tests are organized in the test folder, located at the same level as the src folder in the checkout. From there down the tests attempt to mirror the package structure of the classes that they will be used to test. When new tests are added they should be added according to this pattern; they should be added in a parallel folder of the test directory.
Since Simple Cart addresses several concerns through its various layers, these concerns should be tested separately. For more information about the layers that make up Simple Cart see ???. These concerns and their relation to tests are briefly discussed below.
Simple Cart is based on the Model View Controller (MVC) pattern and the Business Logic addresses the Model Component. Business Logic includes everything calculates, decides and is governed by business rules. Examples might be calculation of tax or shipping, rules defining how promotions are handled and how passwords are set, stored and reset.
Since Business Logic addresses business rules, the tests should be designed to prove that under all circumstances the business rules are enforced. In general all business objects will be written as interfaces, and the tests should be written to test the interface, not the particulars of the implementation. This way the test might be applied to future or alternative implementations with very little effort.
While persistence falls within its own layer, and is not mentioned in the MVC pattern, it is essential to the functionality of Simple Cart. Persistence concerns itself with the translation of an in memory object to a disk based representation. In some cases this may be Relational Database Management System (RDBMS), while in others it may be XML or some other data source.
Persistence related testing should ensure that a particular object in memory can be saved to a data source and later retrieved in the same state. Particular attention should be given to objects that represent monetary units (i.e. Dollars, Yen, Pounds, etc.), or monetary values. Where necessary tests should also ensure that all permitted characters are stored and retrieved correctly, such as may be related to internationalization.
Struts serves the role of Controller in the MVC pattern. As such Struts should never implement any Business Logic and should not contain any presentation specific code. The idea with the controller is to ensure the following: “
Receives the data it needs from the request or session
Handles cases properly where required input validation is missing or invalid
Calls the model passing the correct data
Marshals and transforms as needed the data from the model
Stores the data in the appropriate context or scope
Returns the correct action response
The project StrutsTestCase extends JUnit and provides mock objects that allow controllers to be tested outside a servlet container. The project is hosted at SourceForge and can be found here.