Responding to Reported Failures
Imagine that this process has been running for about 30 days, and that the development team just received a new requirement to implement a discount policy25 percent of the discount will be applied to the total price if the weighted average price per individual item is greater than $20.
To implement this requirement, the getSubTotal() method of the com.ibatis.jpetstore.domain.Cart class needs to be changed. Listing Four (available online) implements the new idea. You can change this code without fear of unknowingly introducing problems. The regression test suite alerts you if your code modifications introduced any undesired results.
When you rerun the regression suite against the modified code, you get one assertion failure and one case where the test fails due to an ArithmeticException; see Figure 3.
Taking a closer look at the ArithmeticException failure, you notice that the new code is introducing a defect because it is not properly handling a case when the number of items in the cart is 0. The problematic line is marked with the comment //Division by zero. The code should be modified to check whether the total number of items in the cart is not equal to 0 before it proceeds to perform the division. For example:
if (totalItems !=0) { BigDecimal actualAvePrice = subTotal.divide(new BigDecimal (totalItems)); if (actualAvePrice.compareTo (targetAvePrice) > 0) { //if avg price is $20 or more, let's apply 25% discount BigDecimal sb = new BigDecimal(0.75); subTotal = subTotal.multiply (sb).setScale(2); } }
When you rerun the regression test suite, the ArithmeticException failure is no longer reported. In this case, the regression suite prevented you from introducing a defect into the code.
Next look at the reported assertion failure. It indicates that a test case was expecting the order total to be $112, but it was actually $84. This failure occurs in the functional BuyDogandCatTest test case (Listing Three), which was added to the regression suite.
Given the new discount policy in place, this is actually the correct behaviornot a defect. This means that the test needs to be updated to remain in sync with the source code change. In this case, the expected amount in the assertion needs to be changed from $112 to $84. If you rerun the regression suite after this change is made, no failures are reported.