Skip to content

Instantly share code, notes, and snippets.

@ju-la-berger
Last active September 21, 2017 15:14
Show Gist options
  • Save ju-la-berger/902abcd0a1292b529018f5a8cb95d642 to your computer and use it in GitHub Desktop.
Save ju-la-berger/902abcd0a1292b529018f5a8cb95d642 to your computer and use it in GitHub Desktop.
Create Spring Environment from Resource (e.g. for unit testing)
package com.github.ju-la-berger;
import java.io.IOException;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.io.support.ResourcePropertySource;
public class EnvironmentFromResource extends AbstractEnvironment {
public EnvironmentFromResource(final String location) {
super();
try {
final ResourcePropertySource propertySource = new ResourcePropertySource(location);
getPropertySources().addLast(propertySource);
} catch (final IOException e) {
throw new RuntimeException(e);
}
}
public EnvironmentFromResource() {
this("classpath:application.properties");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment