Channels ▼
RSS

Web Development

Change Code Without Fear

Source Code Accompanies This Article. Download It Now.


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 policy—25 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.

[Click image to view at full size]

Figure 3: Running the regression suite against modified code.

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 behavior—not 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.


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 
Dr. Dobb's TV