pivotal-cf / java-cfenv

Apache License 2.0
91 stars 56 forks source link

CfEnv: allow passing in values of VCAP_SERVICES & VCAP_APPLICATION #186

Closed andreasf closed 2 years ago

andreasf commented 2 years ago

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 commented 2 years ago

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