How to Register EnvironmentPostProcessors in Auto-Configuration Tests
search cancel

How to Register EnvironmentPostProcessors in Auto-Configuration Tests

book

Article ID: 436650

calendar_today

Updated On:

Products

VMware Tanzu Platform Spring

Issue/Introduction

When testing auto-configuration classes using ApplicationContextRunner (ACR), EnvironmentPostProcessor (EPP) implementations registered in META-INF/spring.factories are ignored.

Auto-configuration tests are designed for isolation. While a standard @SpringBootTest triggers the full SpringApplication.run() lifecycle (which includes SpringFactoriesLoader searching for EPPs), the ACR manually prepares a context. It skips the Bootstrap phase, meaning the Environment is never passed to the factories-loaded EPPs. Consequently, any properties or profiles your EPP should have contributed are missing. 

Resolution

ApplicationContextRunner.withInitializer() to manually call your EPP. Inside the initializer callback, you invoke postProcessEnviroment() directly, injecting the needed properties into the test context's Environment before auto-configuration runs.

In this way, your auto-configuration classes see the properties the EPP would have set, without needing the full application startup - keeping the test lightweight and focus on the specific auto-configuration behavior you want to verify.