unit-test-config-properties

Provides patterns for unit testing `@ConfigurationProperties` classes with `@ConfigurationPropertiesTest`. Validates property binding, tests validation constraints, verifies default values, checks type conversions, and mocks property sources for Spring Boot configuration properties. Use when testing

By giuseppe-trisciuoglio · 2,901 installs

npx skills add giuseppe-trisciuoglio/developer-kit --skill unit-test-config-properties

Source repository · Upstream listing

Unit Testing Configuration Properties and Profiles Overview This skill provides patterns for unit testing @ConfigurationProperties bindings, environment specific configurations, and property validation using JUnit 5. Covers testing property name mapping, type conversions, validation constraints, nested structures, and profile specific configurations without full Spring context startup. Key validation checkpoints: Property prefix matches between @ConfigurationProperties and test properties Validation triggers on @Validated classes with invalid values Type conversions work for Duration, DataSize, collections, and maps When to Use Testing @ConfigurationProperties property binding Testing property name mapping and type conversions Validating configuration with @NotBlank , @Min , @Max , @Email constraints Testing environment specific configurations (dev, prod) Testing nested property structures and collections Verifying default values when properties are not specified Fast configuration tests without Spring context startup Instructions 1. Set up test dependencies : Add spring boot starter test and AssertJ dependencies 2. Use ApplicationContextRunner : Test property bindings without starting full Spring context 3. Define property prefixes : Ensure @ConfigurationProperties(prefix = "...") matches test property paths 4. Test all property paths : Verify each property including nested structures and collections 5. Test validation constraints : Use context.hasFailed() to verify @Validated properties reject invalid values 6. Test type conversions : Verify Duration ( 30s ), DataSize ( 50MB ), collections, and maps convert correctly 7. Test default values : Verify properties have correct defaults when not specified in test properties 8. Test profile specific configs : Use @Profile with ApplicationContextRunner for environment specific configurations 9. Test edge cases : Include empty strings, null values, and type mismatches Troubleshooting flow: If properties don't bind → Check prefix matches (kebab case to camelCase conversion) If validation doesn't trigger → Verify @Validated annotation is present If context fails to start → Check dependencies and @ConfigurationProperties class structure Examples Setup: Test Dependencies Basic Pattern: Property Binding Validation Testing Type Conversion Testing For nested properties , profile specific configurations , collection binding , and advanced validation patterns , see references/advanced examples.md . Best Practices Test all property bindings including nested structures and collections Test validation constraints for all @NotBlank , @Min , @Max , @Email , @Positive annotations Test both default and custom values to verify fallback behavior Use ApplicationContextRunner for fast context free testing Test profile specific configurations separately with @Profile Verify type conversions for Duration, DataSize, collections, and maps Test edge cases : empty strings, null values, type mismatches, out of range values Constraints and Warnings Kebab case to camelCase : Property app.my property maps to myProperty in Java Loose binding : Spring Boot uses loose binding by default; use strict binding if needed @Validated required : Add @Validated annotation to enable constraint validation @ConstructorBinding : All parameters must be bindable when using constructor binding List indexing : Use [0] , [1] notation; ensure sequential indexing for lists Duration format : Accepts ISO 8601 ( PT30S ) or simple syntax ( 30s , 1m , 2h ) Context isolation : Each ApplicationContextRunner creates a new context with no shared state Profile activation : Use spring.profiles.active=profileName in withPropertyValues() for profile tests Troubleshooting Issue Cause Solution Properties not binding Prefix mismatch Verify @ConfigurationProperties(prefix="...") matches property paths Validation not triggered Missing @Validated Add @Validated annotation to configuration class Context fails to start Missing dependencies Ensure spring boot starter test is in test scope Nested properties null Inner class missing Use @Data on nested classes or provide getters/setters Collection binding fails Wrong indexing Use [0] , [1] notation, not (0) , (1)