- Create and verify prerequisite state for the action to be tested
- Execute the action to be tested
- Verify post-action state
Example
Let's say we have a system that allows warehouse employees to ship products. A ShippingOrder is an object in the system that contains the products to be shipped. Our business rule states that a ShippingOrder can only be processed (shipped) if all products in the ShippingOrder are in stock. In other words, we want to verify that either all products in a ShippingOrder are shipped or none of the products in the ShippingOrder are shipped based on stock availability. Our Unit Test will verify this behavior of the ShippingOrder.
1. Create and verify prerequisite state Before a test is executed, we want to ensure that the environment in which a test is about to be executed is known and verified. It is important to stress that it is not sufficient to bring the test environment into a known state but also to explicitly verify the state before execution of the action. This is typically done using ASSERTs provided by the Unit Testing framework. In our example, we have at least three stock level conditions:
S_1. All Products in the ShippingOrder are in stock S_2. None of the Products in the ShippingOrder are in stock
S_3. Some the products in the ShippingOrder are in stock
We have identified three states of product stock levels and hence we will be executing at least three tests. Before each of the three tests is executed, the corresponding state of the stock levels of products in the ShippingOrder must be established and verified by our test setup code. Our test code will create three distinct ShippingOrder objects corresponding to these states. Lets call these objects SO_1, SO_2, and SO_3. The test setup code must also verify that these three ShippingOrder objects are in Pending state.2. Execute the test
Our test is going to verify that a ShippingOrder can only transition to a Shipped state when our business rules are satisfied. We will be writing at least 3 actions:
T_1. Use SO_1. Try to transition ShippingOrder to a Shipped state.
T_2. Use SO_2. Try to transition ShippingOrder to a Shipped state.
T_3. Use SO_3. Try to transition ShippingOrder to a Shipped state.
3. Verify post-action State Once the test action is executed, we verify the post-action state of SO_1, SO_2, and SO_3 using ASSERTs provided by the Unit Testing framework:
V_1. Verify SO_1 is in Shipped state V_2. Verify SO_2 is in Pending state
V_3. Verify SO_3 is in Pending state