← Back to Module 1

Testing Functional Use Cases

Introduction

Testing functional use cases is a crucial part of software development. It ensures that the software behaves as expected and meets the specified requirements. This guide will help you understand how to write and execute tests for functional use cases, with a focus on Java applications.

Functional testing verifies that your application implements the behaviors outlined in your functional requirements. By testing each use case, you confirm that the system works correctly from the user's perspective and that all business requirements are satisfied.

Requirements Traceability

A key aspect of effective functional testing is maintaining traceability between your requirements, use cases, and test cases:

Requirements Traceability Matrix Example

Requirement ID Requirement Description Use Case ID Test Case IDs
REQ-001 Users must be able to place lunch orders UC-001 TC-001, TC-002, TC-003
REQ-002 Users must be able to filter menu by dietary restrictions UC-002 TC-004, TC-005
REQ-003 Users must be able to cancel orders within policy timeframe UC-003 TC-006, TC-007

Test Planning

Before writing tests, it's important to create a test plan that outlines:

A well-structured test plan enables efficient verification of all functional requirements and provides clear guidance for the testing team.

Writing Test Cases

A good test case should include:

Example Test Case

User Login Test

Test Case ID: TC001

Description: Verify successful user login with valid credentials

Requirements Reference: REQ-004 (User Authentication)

Preconditions:

  • User account exists in the system with username "john.doe@example.com" and password "Secure123!"
  • User is not already logged in
  • System is accessible and login page is available

Test Steps:

  1. Navigate to login page at https://example.com/login
  2. Enter email address: john.doe@example.com
  3. Enter password: Secure123!
  4. Click login button

Expected Results:

  • User is successfully logged in
  • User is redirected to dashboard at https://example.com/dashboard
  • Dashboard displays user's name "John Doe"
  • Session cookie is created with appropriate expiration
  • Login event is recorded in system logs

Test Data:

  • Username: john.doe@example.com
  • Password: Secure123!

Test Implementation in Java

In Java applications, you can implement functional tests using frameworks like JUnit, TestNG, and Selenium. Here's an example of a JUnit test that verifies a login functionality:

Java Test Implementation Example

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

/**
 * Tests for the UserAuthenticationService
 * Covers requirement REQ-004: User Authentication
 */
public class UserAuthenticationTest {
    
    private UserAuthenticationService authService;
    private UserRepository userRepository;
    
    @BeforeEach
    void setUp() {
        // Set up test dependencies
        userRepository = new InMemoryUserRepository();
        authService = new UserAuthenticationService(userRepository);
        
        // Create test user
        User testUser = new User("john.doe@example.com", "Secure123!");
        userRepository.save(testUser);
    }
    
    /**
     * Test case ID: TC001
     * Verifies successful login with valid credentials
     */
    @Test
    void loginWithValidCredentials_shouldReturnAuthenticatedUser() {
        // GIVEN
        String email = "john.doe@example.com";
        String password = "Secure123!";
        
        // WHEN
        AuthenticationResult result = authService.login(email, password);
        
        // THEN
        assertTrue(result.isSuccess());
        assertNotNull(result.getUser());
        assertEquals(email, result.getUser().getEmail());
        assertNotNull(result.getSessionToken());
        assertTrue(result.getSessionToken().length() > 0);
    }
    
    /**
     * Test case ID: TC002
     * Verifies failed login with invalid password
     */
    @Test
    void loginWithInvalidPassword_shouldReturnAuthenticationFailure() {
        // GIVEN
        String email = "john.doe@example.com";
        String invalidPassword = "WrongPassword123";
        
        // WHEN
        AuthenticationResult result = authService.login(email, invalidPassword);
        
        // THEN
        assertFalse(result.isSuccess());
        assertNull(result.getUser());
        assertNull(result.getSessionToken());
        assertEquals("Invalid credentials", result.getErrorMessage());
    }
    
    /**
     * Test case ID: TC003
     * Verifies failed login with non-existent user
     */
    @Test
    void loginWithNonExistentUser_shouldReturnAuthenticationFailure() {
        // GIVEN
        String nonExistentEmail = "nobody@example.com";
        String password = "AnyPassword123";
        
        // WHEN
        AuthenticationResult result = authService.login(nonExistentEmail, password);
        
        // THEN
        assertFalse(result.isSuccess());
        assertNull(result.getUser());
        assertNull(result.getSessionToken());
        assertEquals("User not found", result.getErrorMessage());
    }
}
                

Notice how this test class follows best practices:

Executing Tests

When executing tests:

Test Coverage

Ensure your test suite covers:

Use a coverage analysis tool to identify gaps in your testing. For Java applications, tools like JaCoCo can help measure code coverage of your tests.

Test Automation

While manual testing is valuable, automating functional tests provides several benefits:

For Java applications, consider these automation frameworks:

Video Content

Additional Resources