← Back to Guided Projects

Module 3 Guided Project

Unit Testing Practice

In this guided project, you'll learn how to write effective unit tests using JUnit. You'll practice testing various components of an application, creating test cases for different scenarios, and verifying expected behavior.

Understanding Unit Testing

Unit testing is the process of testing individual components or units of code in isolation. A "unit" is typically a method or function that performs a specific task. The goal is to verify that each unit works correctly on its own before integrating it with other units.

The GIVEN-WHEN-THEN Pattern

This project follows the GIVEN-WHEN-THEN pattern for writing clear, focused unit tests:

  • GIVEN: Set up the test conditions and initial state
  • WHEN: Execute the method being tested
  • THEN: Verify the expected results

Test Case Example

@Test
public void calculateTotal_withValidItems_returnsCorrectSum() {
    // GIVEN
    ShoppingCart cart = new ShoppingCart();
    cart.addItem(new Item("Book", 15.99));
    cart.addItem(new Item("Pen", 1.29));
    
    // WHEN
    double total = cart.calculateTotal();
    
    // THEN
    assertEquals(17.28, total, 0.01, "Total should be sum of all item prices");
}
                

Testing Edge Cases

A thorough test suite should include tests for edge cases such as:

  • Empty collections
  • Null inputs
  • Boundary values
  • Invalid inputs that should throw exceptions

Example Edge Case Test

@Test
public void calculateTotal_withEmptyCart_returnsZero() {
    // GIVEN
    ShoppingCart cart = new ShoppingCart();
    
    // WHEN
    double total = cart.calculateTotal();
    
    // THEN
    assertEquals(0.0, total, 0.01, "Empty cart should return zero total");
}

@Test
public void addItem_withNullItem_throwsIllegalArgumentException() {
    // GIVEN
    ShoppingCart cart = new ShoppingCart();
    
    // WHEN/THEN
    assertThrows(IllegalArgumentException.class, 
                 () -> cart.addItem(null),
                 "Adding null item should throw IllegalArgumentException");
}
                

Project Steps

  1. Understand the ATA Math Library

    Review the code for the ATA Math Library to understand what functionality needs to be tested. Focus on the core mathematical operations provided by the library.

  2. Set up the test environment

    Create a test class structure with JUnit 5 annotations. Make sure to organize your tests logically, grouping related tests together.

  3. Write tests for happy path scenarios

    Create tests that verify the correct behavior for expected inputs. For example, test that addition of two positive numbers produces the correct sum.

  4. Test edge cases

    Write tests for edge cases such as zero values, negative numbers, very large numbers, and special cases specific to each math operation.

  5. Test error handling

    Verify that the library properly handles invalid inputs, such as division by zero or calculations that would result in overflow.

  6. Implement tests for the Subscribe and Save application

    Apply what you've learned to write tests for the Subscribe and Save application, which has more complex business logic and dependencies.

  7. Run tests and analyze results

    Execute your test suite, analyze any failures, and refine your tests as needed to ensure comprehensive coverage.