Skip to content

Conversation

andreasf
Copy link
Contributor

@andreasf andreasf commented Jun 21, 2022

This change makes it easier to test components using CfEnv to parse a given VCAP_SERVICES string.

As can be seen in CfEnv's tests, it is possible to mock System.getenv with JMockit, but patching globally scoped objects is quite invasive and also requires configuring a Java agent.

This change adds a second constructor to CfEnv, so that the VCAP_SERVICES value can simply be passed in.

A Spring Configuration class like the following...

@Configuration
class FooConfiguration {
    public FooConfiguration(Environment environment) {
        this.environment = environment;
    }

    @Bean
    public Foo foo() {
        CfEnv cfEnv = new CfEnv(environment.get("VCAP_APPLICATION"), environment.get("VCAP_SERVICES"));
        ...
    }
}

...can then be easily tested using MockEnvironment.

@andreasf
Copy link
Contributor Author

I'll amend the commit to fix the checkstyle issues. Also feel free to reach out on Slack.

This change makes it easier to test components using CfEnv to parse a given
VCAP_SERVICES string.
@andreasf andreasf force-pushed the allow_passing_vcap_values branch from 1161fa1 to 1315e22 Compare June 28, 2022 14:52
@pivotal-david-osullivan pivotal-david-osullivan merged commit bd408ce into pivotal-cf:main Jun 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants