diff --git a/docs/test-coverage-improvements.md b/docs/test-coverage-improvements.md new file mode 100644 index 00000000000..8e84d2c3303 --- /dev/null +++ b/docs/test-coverage-improvements.md @@ -0,0 +1,33 @@ +# Test Coverage Improvements + +## Overview +This document outlines the test coverage improvements made to the Spring PetClinic application. Two new test classes were added to increase test coverage for previously untested components. + +## New Test Classes + +### 1. PetClinicRuntimeHintsTests +**File:** `src/test/java/org/springframework/samples/petclinic/PetClinicRuntimeHintsTests.java` + +This test class verifies the functionality of the `PetClinicRuntimeHints` class, which is responsible for registering runtime hints for resources and serialization types. The test class includes: + +- `shouldRegisterResourcePatterns()`: Tests that specific resource patterns (db/*, messages/*, mysql-default-conf) are correctly registered in the RuntimeHints. +- `shouldRegisterSerializationTypes()`: Tests that no exceptions are thrown when registering serialization types for `BaseEntity`, `Person`, and `Vet` classes. + +### 2. PetClinicApplicationTests +**File:** `src/test/java/org/springframework/samples/petclinic/PetClinicApplicationTests.java` + +This test class verifies that the Spring Boot application context loads correctly. It includes: + +- `contextLoads()`: A simple test that will fail if the application context cannot be loaded properly. This ensures that the main application class `PetClinicApplication` and its configuration are working correctly. + +## Benefits + +These new test classes provide the following benefits: + +1. **Increased Test Coverage**: Previously untested components now have test coverage, reducing the risk of undetected issues. +2. **Verification of Critical Components**: The tests verify that critical components like runtime hints registration and application context loading work correctly. +3. **Documentation Through Tests**: The tests serve as documentation for how these components are expected to function. + +## Validation + +All tests have been executed and passed successfully without any Checkstyle violations, confirming the correctness of the implementation. diff --git a/src/test/java/org/springframework/samples/petclinic/PetClinicApplicationTests.java b/src/test/java/org/springframework/samples/petclinic/PetClinicApplicationTests.java new file mode 100644 index 00000000000..fb55154c2a0 --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/PetClinicApplicationTests.java @@ -0,0 +1,17 @@ +package org.springframework.samples.petclinic; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +/** + * Test class for {@link PetClinicApplication} + */ +@SpringBootTest +class PetClinicApplicationTests { + + @Test + void contextLoads() { + // This test will fail if the application context cannot be loaded + } + +} diff --git a/src/test/java/org/springframework/samples/petclinic/PetClinicRuntimeHintsTests.java b/src/test/java/org/springframework/samples/petclinic/PetClinicRuntimeHintsTests.java new file mode 100644 index 00000000000..ab4e541eeaa --- /dev/null +++ b/src/test/java/org/springframework/samples/petclinic/PetClinicRuntimeHintsTests.java @@ -0,0 +1,38 @@ +package org.springframework.samples.petclinic; + +import org.junit.jupiter.api.Test; +import org.springframework.aot.hint.RuntimeHints; +import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; +import org.springframework.samples.petclinic.model.BaseEntity; +import org.springframework.samples.petclinic.model.Person; +import org.springframework.samples.petclinic.vet.Vet; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Test class for {@link PetClinicRuntimeHints} + */ +class PetClinicRuntimeHintsTests { + + @Test + void shouldRegisterResourcePatterns() { + RuntimeHints hints = new RuntimeHints(); + PetClinicRuntimeHints petClinicRuntimeHints = new PetClinicRuntimeHints(); + + petClinicRuntimeHints.registerHints(hints, getClass().getClassLoader()); + + assertThat(RuntimeHintsPredicates.resource().forResource("db/something")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("messages/something")).accepts(hints); + assertThat(RuntimeHintsPredicates.resource().forResource("mysql-default-conf")).accepts(hints); + } + + @Test + void shouldRegisterSerializationTypes() { + RuntimeHints hints = new RuntimeHints(); + PetClinicRuntimeHints petClinicRuntimeHints = new PetClinicRuntimeHints(); + + // This test verifies that no exceptions are thrown when registering hints + petClinicRuntimeHints.registerHints(hints, getClass().getClassLoader()); + } + +}