Back to Repositories

Testing Exception Handling Implementation in Stirling-PDF

This test suite validates the error handling utilities in the Stirling-PDF application, focusing on exception management and model attribute population. The tests ensure proper conversion of exceptions into both Model and ModelAndView objects for consistent error handling across the application.

Test Coverage Overview

The test suite provides comprehensive coverage of the ErrorUtils class functionality, specifically testing exception handling and model population.

  • Tests exception to Model conversion with message and stack trace validation
  • Verifies ModelAndView generation from exceptions
  • Ensures proper attribute population in both Model and ModelAndView objects

Implementation Analysis

The implementation follows JUnit 5 testing patterns with a focus on clear, isolated test cases. Each test method validates specific conversion functionality using mock Model objects and controlled exception scenarios.

The testing approach leverages Spring Framework’s ExtendedModelMap for realistic model interaction testing.

Technical Details

  • JUnit Jupiter framework for test execution
  • Spring Framework testing utilities
  • Mock objects using ExtendedModelMap
  • Static assertion methods for verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices including clear test method naming, proper test isolation, and comprehensive assertion coverage.

  • Isolated test scenarios with controlled inputs
  • Thorough verification of output attributes
  • Clear separation of test setup, execution, and verification

stirling-tools/stirling-pdf

src/test/java/stirling/software/SPDF/utils/ErrorUtilsTest.java

            
package stirling.software.SPDF.utils;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import org.junit.jupiter.api.Test;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;

public class ErrorUtilsTest {

    @Test
    public void testExceptionToModel() {
        // Create a mock Model
        Model model = new org.springframework.ui.ExtendedModelMap();

        // Create a test exception
        Exception ex = new Exception("Test Exception");

        // Call the method under test
        Model resultModel = ErrorUtils.exceptionToModel(model, ex);

        // Verify the result
        assertNotNull(resultModel);
        assertEquals("Test Exception", resultModel.getAttribute("errorMessage"));
        assertNotNull(resultModel.getAttribute("stackTrace"));
    }

    @Test
    public void testExceptionToModelView() {
        // Create a mock Model
        Model model = new org.springframework.ui.ExtendedModelMap();

        // Create a test exception
        Exception ex = new Exception("Test Exception");

        // Call the method under test
        ModelAndView modelAndView = ErrorUtils.exceptionToModelView(model, ex);

        // Verify the result
        assertNotNull(modelAndView);
        assertEquals("Test Exception", modelAndView.getModel().get("errorMessage"));
        assertNotNull(modelAndView.getModel().get("stackTrace"));
    }
}