Second Guess The Calculation

Definition:

  • Where rather than using concrete test data, we use something that needs us to calculate the correct answer ahead of assertion

Code Example:

@Test
public void testCalculatePrice() {
    Order order = new Order();
    order.addProduct(new Product("Product A", 10.0, 2));
    order.addProduct(new Product("Product B", 5.0, 3));

    double expectedPrice = 10.0 * 2 + 5.0 * 3;
    double actualPrice = order.calculatePrice();

    assertEquals(expectedPrice, actualPrice, 0.001);
}

References:

Quality attributes

  • - Code Example

  • - Cause and Effect

  • - Frequency

  • - Refactoring